Tic-Tac-Toe | programming_info || Python_problem-04

 Rules for the game:

1.Display game board with 9 position for x and o

2.Make sure the board is empty and the first player to start

3.Numbered cells for each player turn

4.provide error message for placing a number in filled cell

5. If a player scores a line of 3 symbols provide "You Won" message

Code using python:

theBoard = {'7': ' ', '8': ' ', '9': ' ',

            '4': ' ', '5': ' ', '6': ' ',

            '1': ' ', '2': ' ', '3': ' '}

boardKeys = []


# print(boardkeys)

for key in theBoard:

    boardKeys.append(key)


# print(boardkeys)

def printBoard(board):

    print(board['7'] + '/' + board['8'] + '/' + board['9'])

    print('-/-/-')

    print(board['4'] + '/' + board['5'] + '/' + board['6'])

    print('-/-/-')

    print(board['1'] + '/' + board['2'] + '/' + board['3'])

    print('-/-/-')


# printBoard(theBoard)

def game():

    turn = 'X'

    count = 0

    for i in range(10):

        printBoard(theBoard)

        print(" It is turn of" + "turn" + "Specify the place you want to go ")

        move = input()

        if theBoard[move] == ' ':

            theBoard[move] = turn

            count += 1

        else:

            print("Sorry this cell location is filled.Please choose another one")

            continue

        if count >=5:

            if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

            if theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

            if theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

            if theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

            if theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

            if theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

            if theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

            if theBoard['3'] == theBoard['5'] == theBoard['7'] != ' ':

                printBoard(theBoard)

                print("Player " + "won the game")

                break

        if count ==9:

            print("\n Game over")

            print("The game is tie!")

        if turn == 'X':

            turn = '0'

        else:

            turn = 'X'

    restart = input("Do u want to restart the game? (y/n)")

    if restart == 'y' or restart =='Y':

        for key in boardKeys:

            theBoard[key] = ' '

        game()

if _name_ =="_main_":

    game()

Share:

1 comment:

  1. Hi TicTacToe is basically Noughts and Crosses game.Attention Tic Tac Toe lovers now you can play it online by click here.You can play it individually or with computer.

    ReplyDelete

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support