My First Python Project
This project description taken from cisco learning website https://skillsforall.com/launch?id=da0847b7-e6fc-4597-bc31-38ddd6b07a2e&tab=curriculum&view=3084e7a7-3c28-5dab-a765-67e23da7dc19
I only read the following description and started coding in Python myself I only hint and guidance for only following three line
from randon import randrage
randrange(1,9)
Remaing program developed my self without copying..
After description of program Coding is given.
Scenario
Your task is to write a simple program which pretends to play tic-tac-toe with the user. To make it all easier for you, we've decided to simplify the game. Here are our assumptions:
- the computer (i.e., your program) should play the game using
'X' s; - the user (e.g., you) should play the game using
'O' s; - the first move belongs to the computer − it always puts its first
'X' in the middle of the board; - all the squares are numbered row by row starting with
1 (see the example session below for reference) - the user inputs their move by entering the number of the square they choose − the number must be valid, i.e., it must be an integer, it must be greater than
0 and less than10 , and it cannot point to a field which is already occupied; - the program checks if the game is over − there are four possible verdicts: the game should continue, the game ends with a tie, you win, or the computer wins;
- the computer responds with its move and the check is repeated;
- don't implement any form of artificial intelligence − a random field choice made by the computer is good enough for the game.
The example session with the program may look as follows:
1 2 3
4 X 6
7 8 9
---------
Enter Your Move==> 1
####### Starting of Coding From Here ######################
from random import randrange
l=[1,2,3,4,5,6,7,8,9]
l[4]='X'
def win():
# 3 rows simply
if l[0] == l[1] and l[1] == l[2]:
return True
if l[3] == l[4] and l[4] == l[5]:
return True
if l[6] == l[7] and l[7] == l[8]:
return True
# 3 colums
if l[0] == l[3] and l[3] == l[6]:
return True
if l[1] == l[4] and l[4] == l[7]:
return True
if l[2] == l[5] and l[5] == l[8]:
return True
# 2 diagonals
if l[0] == l[4] and l[4] == l[8]:
return True
if l[2] == l[4] and l[4] == l[6]:
return True
return False
def show():
print(' ',l[0],end=' ')
print(l[1],end=' ')
print(l[2])
print()
print(' ',l[3],end=' ')
print(l[4],end=' ')
print(l[5])
print()
print(' ',l[6],end=' ')
print(l[7],end=' ')
print(l[8])
print(' -------')
def draw():
for i in l:
if type(i) == int:
return False
return True # no one value is type integre
while True:
show()
you = int(input(' Enter Your Move==> '))
if you == 0:
print('Ok Bye Bye!!')
break
if type(l[you-1]) != int:
print('Aleady Occupied Reenter Please. Press any key to continuee.')
input()
continue
l[you-1]='O'
if win():
show()
print('Congrats: You WIN Prize')
break
while True:
comp = randrange(1,9)
if type(l[comp]) == int:
break
l[comp]='X'
if win():
show()
print('SORRY! YOU LOOOOOSE Computer:WINS')
break
if draw():
show()
print('GAME IS DRAW No one WIN')
break
################ END OF CODING OF PROJECT TAC TIE TOE
Now I am copying pasting actual Python coding written by professional by cisco and skillforall.com to see the difference
###################### STARTING OF skillforall.com Coding by cisco TO SEE difference===============
from random import randrange
def display_board(board):
print("+-------" * 3,"+", sep="")
for row in range(3):
print("| " * 3,"|", sep="")
for col in range(3):
print("| " + str(board[row][col]) + " ", end="")
print("|")
print("| " * 3,"|",sep="")
print("+-------" * 3,"+",sep="")
def enter_move(board):
ok = False # fake assumption - we need it to enter the loop
while not ok:
move = input("Enter your move: ")
ok = len(move) == 1 and move >= '1' and move <= '9' # is user's input valid?
if not ok:
print("Bad move - repeat your input!") # no, it isn't - do the input again
continue
move = int(move) - 1 # cell's number from 0 to 8
row = move // 3 # cell's row
col = move % 3 # cell's column
sign = board[row][col] # check the selected square
ok = sign not in ['O','X']
if not ok: # it's occupied - to the input again
print("Field already occupied - repeat your input!")
continue
board[row][col] = 'O' # set '0' at the selected square
def make_list_of_free_fields(board):
free = [] # the list is empty initially
for row in range(3): # iterate through rows
for col in range(3): # iterate through columns
if board[row][col] not in ['O','X']: # is the cell free?
free.append((row,col)) # yes, it is - append new tuple to the list
return free
def victory_for(board,sgn):
if sgn == "X": # are we looking for X?
who = 'me' # yes - it's computer's side
elif sgn == "O": # ... or for O?
who = 'you' # yes - it's our side
else:
who = None # we should not fall here!
cross1 = cross2 = True # for diagonals
for rc in range(3):
if board[rc][0] == sgn and board[rc][1] == sgn and board[rc][2] == sgn: # check row rc
return who
if board[0][rc] == sgn and board[1][rc] == sgn and board[2][rc] == sgn: # check column rc
return who
if board[rc][rc] != sgn: # check 1st diagonal
cross1 = False
if board[2 - rc][2 - rc] != sgn: # check 2nd diagonal
cross2 = False
if cross1 or cross2:
return who
return None
def draw_move(board):
free = make_list_of_free_fields(board) # make a list of free fields
cnt = len(free)
if cnt > 0: # if the list is not empty, choose a place for 'X' and set it
this = randrange(cnt)
row, col = free[this]
board[row][col] = 'X'
board = [ [3 * j + i + 1 for i in range(3)] for j in range(3) ] # make an empty board
board[1][1] = 'X' # set first 'X' in the middle
free = make_list_of_free_fields(board)
human_turn = True # which turn is it now?
while len(free):
display_board(board)
if human_turn:
enter_move(board)
victor = victory_for(board,'O')
else:
draw_move(board)
victor = victory_for(board,'X')
if victor != None:
break
human_turn = not human_turn
free = make_list_of_free_fields(board)
display_board(board)
if victor == 'you':
print("You won!")
elif victor == 'me':
print("I won")
else:
print("Tie!")
######################## END OF Python Coding of the project by skillforall.com by cisco =====
Comments
Post a Comment