Question: Python, please! I did the code all I need is for the board to look and work like the example below and a flowchart. This
Python, please! I did the code all I need is for the board to look and work like the example below and a flowchart.
This project is to be done with Python. It should be accompanied by a description outlining which option chosen and a description of how it works and how to use it.
Deliverables:
1. Python program file (.py)
2. Flowchart (.pdf)
3. the CSV file used (if any)
4. Description (.pdf ). This will be used as a user guide. It can be combined with the flowchart in one file.
There is a rubric that will be applied to this assignment. Make sure you check it. If you do not satisfy the criteria, you will not get credit for it. A key outcome for this class is problem solving ability. That outcome applies here as well. Find a way to correctly and materially use the criteria in your programs. Make sure you test your code. The grade factor is a multiplier to you rubric score.
Choose from the following options:
Tic Tac Toe Grade factor 1.15
Create a text board such as this
A B C
1 _ | _ | _
2 _ | _ | _
3 _ | _ | _
This will be a two-dimensional array that will accommodate Xs and Os.
The program will determine who wins and loses.
The program will reject placing an X or O in an occupied spot
The user will play against the computer which will select the best next move.
Criteria
Branching
Looping
Functions
Lists/Arrays
Tuples
Reading/writing from/to a file
Error Trapping/Exception Handling
Formatting Output
Flowchart
Commenting Code
Program Execution
Code:
import random
#tictactoe function def TicTacToe(): cell = [None] + list(range(1, 10)) #winning combinations winning = [ (1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9), (1, 5, 9), (3, 5, 7), ]
#print the cell def display(): print(cell[1], cell[2], cell[3]) print(cell[4], cell[5], cell[6]) print(cell[7], cell[8], cell[9]) print()
#get user move def getMove(): while True: try: a = int(input()) if a in cell: return a else: print(" Invalid move. Try again") except ValueError: print(" That's not a number. Try again")
#check winner def isOver(): for a, b, c in winning: if cell[a] == cell[b] == cell[c]: print("Player {0} wins! ".format(cell[a])) print("Congratulations! ") return True if 9 == sum((pos == 'X' or pos == 'O') for pos in cell): print("The game ends in a tie ") return True
#if not game over get another move for player in 'XO' * 9: display() if isOver(): break if(player=='X'): #user play print("Player {0} move".format(player)) print("Player {0} pick your move.. for hint enter 'a':".format(player)) cell[getMove()] = player print()
if(player=='O'): #computer print("Player {0} move".format(player)) while (True): a=random.randint(0,9) if a in cell: break cell[a] = player print()
while True: TicTacToe() if input("Play again (y/n) ") != "y": break
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
