3/28/2023 0 Comments Arcade library with thonny python![]() This can be done by using a while loop that will check if the player is inputting wrong number or any letter in position number that is not in the range between 1 to 9. If the position number given by the player is already occupied, for eg, if player 1 has chosen position 5 and in next turn player 2 also tries to place marker on the same position, then player 2 will be asked to choose a different position that is not occupied. Next make player_choice() function that will take input from the player regarding where to place marker on the board for the next move if it is a free position. def fullboardcheck(board): for i in range(1,10): if space_free(board, i): return False return True Step 7: The Next Move ![]() Using if function state that if any given position of i on the board is empty, then the board is not full and it will return false. Use for loop that goes for i in range 1 to 10, where i is all the possible positions on the board. In fullboadcheck() function pass board as a parameter. def space_free(board,position): return board=" " Step 6: Full Board Checkĭo full board check to ascertain if all the positions on the board are occupied or not. Define space_free() function in which board and position would be passed as parameters, where it will return boolean true, if board at that position is equal to empty string. In free space check whether any particular position on the board is freely available or not. The design is inspired by the numerical pad on keyboards.Ĭhecking for free space, which means empty positions on the board, is the next step. The markers can be placed on the grid using the numbers 1–9 of the computer keyboard. Equate marker to the position on the board. Position would be the spot where the marker would be placed on the board. In this board would be the tic-tac-toe grid. import random def first_move(): if random.randint(0,1)=0: return "player 2 will start the game" else: return "player 1 will start the game" Step 4: Deciding Where to Move the Markerįor placing the marker on a given position, define a function handle_turn() in which pass 3 parameters board, marker and position. ![]() If it is 0 then player 2 will start the game. If the random number is equal to 1, then player 1 will start the game. Define a function first_move() to generate a random number using random_randint()function between 0 and 1 to decide which player is going to start first. ![]() It is a built-in library in Python, which is used to generate random numbers between two given numbers. Import random library to decide which player will start the game. Start making the tic-tac-toe grid by defining display_board()function, which will display it in a grid format when printed def display_board(board): print(board+"|"+board+"|"+board) print("-|-|-") print(board+"|"+board+"|"+board) print("-|-|-") print(board+"|"+board+"|"+board) print("-|-|-") testing_board=*10 display_board(testing_board) The steps to create this game on Python using some of the basic functions are described in the succeeding steps. In case neither of the players is able to get this, the game then ends in a draw. The player who gets three marks on the board in a row, column, or diagonally, wins. ![]() It is a common 2-player game where one player plays crosses (X) and the other plays naughts(O), each player taking turns to play. As one of my first mini-projects I designed a simple tic-tac-toe game. I have been learning Python for a while now. Projects are a great way to learn, because it gives an opportunity to apply the knowledge acquired as otherwise, it is difficult to retain the same. Once the basic syntaxes are grasped, it’s best to start making own projects. Python is a great language to learn for the beginners. This is an attempt to design the same using Python. With enhancing technologies, naughts and crosses, a very common tic-tac-toe game, has moved from paper to our laptops. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |