0% found this document useful (0 votes)
67 views3 pages

Printboard: Print Print Print Print Print

The document describes a Tic-Tac-Toe game program written in Python. It includes functions to initialize an empty game board dictionary, print the board, and play a game. The game function runs a loop that alternates between asking players X and O to choose a position until someone wins by getting 3 in a row or it's a tie after 9 turns. It checks for wins after every turn and ends the game by printing the winning board if someone wins or a tie message if it reaches 9 turns.

Uploaded by

abhi raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views3 pages

Printboard: Print Print Print Print Print

The document describes a Tic-Tac-Toe game program written in Python. It includes functions to initialize an empty game board dictionary, print the board, and play a game. The game function runs a loop that alternates between asking players X and O to choose a position until someone wins by getting 3 in a row or it's a tie after 9 turns. It checks for wins after every turn and ends the game by printing the winning board if someone wins or a tie message if it reaches 9 turns.

Uploaded by

abhi raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/17/2020 Untitled1

In [ ]:

theBoard = {'7': ' ' , '8': ' ' , '9': ' ' ,
'4': ' ' , '5': ' ' , '6': ' ' ,
'1': ' ' , '2': ' ' , '3': ' ' }

board_keys = []

for key in theBoard:


board_keys.append(key)

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'])

def game():

turn = 'X'
count = 0

for i in range(10):
printBoard(theBoard)
print("It's your turn," + turn + ".Move to which place?")

move = input()

if theBoard[move] == ' ':


theBoard[move] = turn
count += 1
else:
print("That place is already filled.\nMove to which place?")
continue

if count >= 5:
if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the m
iddle
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the b
ottom
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the lef
t side
printBoard(theBoard)
print("\nGame Over.\n")
localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 5/7
4/17/2020 Untitled1

print(" **** " +turn + " won. ****")


break
elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': # down the mid
dle
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the rig
ht side
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break

if count == 9:
print("\nGame Over.\n")
print("It's a Tie!!")

if turn =='X':
turn = 'O'
else:
turn = 'X'

restart = input("Do want to play Again?(y/n)")


if restart == "y" or restart == "Y":
for key in board_keys:
theBoard[key] = " "

game()

if __name__ == "__main__":
game()

localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 6/7
4/17/2020 Untitled1

| |
-+-+-
| |
-+-+-
| |
It's your turn,X.Move to which place?
5
| |
-+-+-
|X|
-+-+-
| |
It's your turn,O.Move to which place?
1
| |
-+-+-
|X|
-+-+-
O| |
It's your turn,X.Move to which place?
1
That place is already filled.
Move to which place?
| |
-+-+-
|X|
-+-+-
O| |
It's your turn,X.Move to which place?
2
| |
-+-+-
|X|
-+-+-
O|X|
It's your turn,O.Move to which place?
4
| |
-+-+-
O|X|
-+-+-
O|X|
It's your turn,X.Move to which place?
8
|X|
-+-+-
O|X|
-+-+-
O|X|

Game Over.

**** X won. ****

In [ ]:

localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 7/7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy