Widget HTML Atas

Use Case Diagram For Snake And Ladder Game

We developed library for this project consists of pretty much all functionality. Following are the functions included in DiceLib.py

In order to run this game you need to import dice library as follows:

Create Table in database

The first function should be called is createtable() looks as follows which will create required table in sqlite3 database where we store players information

            import            DiceLib            DiceLib            .            createtable            ()          

Register User

Use the addmanyuser(user) function to register user at the begining of the game. You can call the function as follows:

            import            DiceLib            user            =            [(            'sumon'            ,            0            ),            (            'mohammed'            ,            0            ),            (            'jannatun'            ,            0            )]            DiceLib            .            addmanyuser            (            user            )          

Using this function you can register more than one players at a time. You can find DiceLib.adduser() function which can be called for registering only one player at a time.

Definition of the addmanyuser(user) function looks as follows:

            def            addmanyuser            (            user            ):            """                          :users:[] dictionary of user(name,score)                          this function will insert multiple user together                          """            c            .            executemany            (            '''INSERT INTO players(name,score)                          VALUES(?,?)'''            ,            user            )            return            True          

Game Start

While start playing first function should be called is getalluser() which will display all the players with initial score of zero. You can call this function as follows:

            def            getalluser            ():            """                          this method will print all the players information registered                          for this session                          """            c            .            execute            (            'SELECT * FROM players ORDER BY 1'            )            for            i            in            c            :            print            "            \n            "            print            "Player "            +            str            (            i            [            0            ])            +            ":"            +            str            (            i            [            1            ])            +            ":"            +            str            (            i            [            2            ])          

This function will fetch all registered players information from database.

Display the Board

Call this display_snakes_and_ladders() function to display snakes and ladders. It will print snakes and ladders and the function looks as follows:

            def            display_snakes_and_ladders            ():            """                          display snakes and ladders                          """            cprint            (            "            \n            Ladders: "            ,            'red'            )            PRINT_RED_ON_BLUE            (            LIST_OF_LADDERS            )            cprint            (            "            \n            Snakes: "            ,            'red'            )            PRINT_RED_ON_BLUE            (            LIST_OF_SNAKES            )            print            "            \n            "          

Next call the function display_board() which will print the initial state board. This function looks like

            def            display_board            ():            """                          this function will display dice board along with player's score                          :return:will display board                          """            num_cols            =            10            num_rows            =            10            cell_index            =            num_cols            *            num_rows            is_row_reverse            =            0            for            row            in            range            (            0            ,            num_rows            ):            col_index            =            0            row            =            []            row_str            =            "       | "            while            col_index            <            num_cols            :            row            .            append            (            cell_index            )            col_index            +=            1            cell_index            -=            1            if            is_row_reverse            ==            1            :            row            .            reverse            ()            is_row_reverse            =            0            ...            ...            ...            code            skipped            print            row_str            print            "       | "            +            "-"            *            57            +            " | "          

This is a quite big function that's why we aren't including the whole code in document. For detail you have to look into the original code file.

Game Control

Call main play_game(total) function which expect total number of players currently going to play. Based on the total number of players it will configure the turn and continue the game. play_game(total) function look as follows:

            def            play_game            (            total            ):            """                          Main method to control game and turn among all registered players                          :param total:                          :return control game and once have winner exit                          """            proceed            =            True            while            proceed            :            for            i            in            range            (            0            ,            total            ):            current_player            =            getusernamebyid            (            i            +            1            )            .            title            ()            raw_input            (            "            \n            "            +            current_player            +            " it's your turn, "            "please hit enter                        \n            "            )            score            =            throw_dice            ()            current_score            =            getuserscorebyid            (            i            +            1            )            print            "You scored : "            +            str            (            score            )            print            "Previous score: "            +            str            (            current_score            )            if            current_score            >            0            :            current_score            =            update_player_score            (            i            +            1            ,            score            ,            '+'            )            else            :            if            score            ==            1            :            current_score            =            update_player_score            (            i            +            1            ,            score            ,            '+'            )            if            str            (            current_score            )            in            LIST_OF_SNAKES            .            keys            ():            score            =            int            (            LIST_OF_SNAKES            [            str            (            current_score            )])            print            (            "OMG !!! it's snake @ "            +            str            (            current_score            )            \            +            " and tail is @ "            +            str            (            score            ),            'red'            )            current_score            =            update_player_score            (            i            +            1            ,            score            ,            's'            )            elif            str            (            current_score            )            in            LIST_OF_LADDERS            .            keys            ():            score            =            int            (            LIST_OF_LADDERS            [            str            (            current_score            )])            print            (            "YES!!! Ladder @ "            +            str            (            current_score            )            \            +            "  and top of the ladder @ "            +            str            (            score            ),            'green'            )            current_score            =            update_player_score            (            i            +            1            ,            score            ,            'l'            )            if            int            (            current_score            )            ==            100            :            proceed            =            False            PRINT_RED_ON_GREEN            (            current_player            +            " You win !!!!"            )            display_board            ()            break            elif            int            (            current_score            )            >            100            :            current_score            =            update_player_score            (            i            +            1            ,            score            ,            '-'            )            print            current_player            +            " your total > 100, deducting current"            \            " points :"            +            str            (            current_score            )            print            current_player            +            " your current score is :"            \            +            str            (            current_score            )            display_snakes_and_ladders            ()            display_board            ()          

These are the following functions will called iteratively from main play_game(total) function till the game ends and we find winner.

            current_player            =            getusernamebyid            (            i            +            1            )            .            title            ()          

Above shows that how we get current player's name in title case.

Update Score

            current_score            =            update_player_score            (            i            +            1            ,            score            ,            '+'            )          

Above function update_player_score(player_id, val, operator) update the score depending on the operator +/-. It also takes the player id as input parameter and number obtained in this throw as val.

Check for Snakes or Ladders

Then following code snippet check whether current score hold any snake or ladder. Depending on snake or ladder it agiain adjust the score.

            if            str            (            current_score            )            in            LIST_OF_SNAKES            .            keys            ():            score            =            int            (            LIST_OF_SNAKES            [            str            (            current_score            )])            print            (            "OMG !!! it's snake @ "            +            str            (            current_score            )            \            +            " and tail is @ "            +            str            (            score            ),            'red'            )            current_score            =            update_player_score            (            i            +            1            ,            score            ,            's'            )            elif            str            (            current_score            )            in            LIST_OF_LADDERS            .            keys            ():            score            =            int            (            LIST_OF_LADDERS            [            str            (            current_score            )])            print            (            "YES!!! Ladder @ "            +            str            (            current_score            )            \            +            "  and top of the ladder @ "            +            str            (            score            ),            'green'            )            current_score            =            update_player_score            (            i            +            1            ,            score            ,            'l'            )          

Find Winner

In order to declare current player winner we use the following code snippet.

            if            int            (            current_score            )            ==            100            :            proceed            =            False            PRINT_RED_ON_GREEN            (            current_player            +            " You win !!!!"            )            display_board            ()            break          

At the same time we display the updated board.

Archive

Once we find the winner immediately control come out of the play_game(total_number_of_players) . Then immediately we call saveuserstoarchive() function looks as follows:

            def            saveuserstoarchive            ():            """                          this function will save current session players to archive table                          for feature reference                          :return:                          """            val            =            get_max_session_id            ()            if            str            (            val            [            0            ])            .            isdigit            ():            flag            =            val            [            0            ]            +            1            else            :            flag            =            1            cursor            =            c            .            execute            (            'SELECT * FROM players ORDER BY 1'            )            data            =            cursor            .            fetchall            ()            for            i            in            data            :            c            .            execute            (            '''INSERT INTO archive(session_id, player_id, name, score)                          VALUES(?,?,?,?)'''            ,            (            flag            ,            i            [            0            ],            i            [            1            ],            i            [            2            ]))            return            True          

Posted by: alvaadwelle0196543.blogspot.com

Source: https://sphinxreadthedocs.readthedocs.io/en/latest/developer-document.html