project1.py - # # CS 177 - Project1.py # Xinyue Yang,...

This preview shows page 1 - 3 out of 7 pages.

## CS 177 - Project1.py# Xinyue Yang, 0030170131# Following Coding Standards and Guidelines# The game can be played by two players or against a computer opponent.# The object of the game is for one player to get three of their markers# in a row either horizontally, vertically or diagonally## Define the main()def main():# Setup a while loop that repeats until the user chooses to end the programnumber = welcome()while True:# Call the welcome() function(returnsnumberofplayers)# Call the game() function to play a gamegame(number)# Prompt the user to determine if they want to play another gamedecision=input('Would you like to play again? (y/n): ')if decision=='y':number = welcome()game(number)# Display “Thank you for playing” messageelse:print('Thank you for playing')break# welcome()def welcome():# Display a greeting and description of the program’s purposetext1='Welcome to Tic-Tac-Toe'text2='----------------------'text3="Try to get three X's or O's in a row!"print(format(text1,'^40'))print(format(text2,'^40'))print(format(text3,'^40'))print('')while True:# Prompt the user to choose between a 1 or 2 player game.number=input(" "*5+ "select a 1 or 2 player game: ")number=int(number)if number == 1 or number == 2:# Display a message confirming their choice of playersprint(' '+str(number)+' Player game selected...')print(' '+'------------------')# Prompt the user to press the ENTER key to continueenter=input(' '+'Press Enter to start the game: ')if enter=='\n':game()break# If the value entered is invalid, continue to prompt the user to answer only with 1 or 2if number != 1 and number !=2:print(' Choice must be 1 or 2 players...')continue
return number# import random for the computer's choice of locationimport random# Define game() functiondef game(number):# Initialize a List of 9 empty Strings representing the 9 squares in the Tic-Tac-Toe boardlist1=[' ',' ',' ',' ',' ',' ',' ',' ',' ']listop = []# Draw the initial (empty) game gridprint(' '*8+' '*4+' 1'+' '*4+' 2'+' '*4+' 3'+' ')print(' '*8+' ----------------- ')print(' '*8+'A |'+' '+list1[0]+' '+' |'+' '+list1[1]+' '+' |'+' '+list1[2]+''+' |')print(' '*8+' ----------------- ')print(' '*8+'B |'+' '+list1[3]+' '+' |'+' '+list1[4]+' '+' |'+' '+list1[5]+''+' |')print(' '*8+' ----------------- ')print(' '*8+'C |'+' '+list1[6]+' '+' |'+' '+list1[7]+' '+' |'+' '+list1[8]+''+' |')print(' '*8+' ----------------- ')a=['a1','a2','a3','b1','b2','b3','c1','c2','c3']# If it's a one player gameif number==1:

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 7 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
N/A
Tags
Carriage return, Enter key

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture