Tic tac toe
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are:
- to build your skill applying computational thinking to a problem
- to assess your knowledge and skills of different programming practices
What is this problem set trying to do[edit]
This problem set is testing your computational thinking, your skill and understanding of lists, and your skill and understanding of loops
The Problem[edit]
Your program must do three things:
- it must allow a human to play against a computer a game of tic tac toe
- your program must have win and lose conditions
- it must draw a tic tac to board, updated with x's and O's as the computer and human play.
- It must follow the rules of tic tac toe
Hacker edition[edit]
In the hacker version:
- Your program should be good at playing tic tac toe
Python Version[edit]
import random
# initialization stuff here:
board = [1,2,3,4,5,6,7,8,9]
computerSpaces = []
humanSpaces =[1]
game = 0
def drawPieces(position):
if position in humanSpaces:
return "X"
elif position in computerSpaces:
return "O"
else:
return position
# this function draws the board
def drawBoard():
print("")
print("")
print(" ",drawPieces(1)," | (2) | (3) ")
print(" | | ")
print("----------------------")
print(" (4) | (5) | (6) ")
print(" | | ")
print("----------------------")
print(" (7) | (8) | (9) ")
print(" | | ")
return
# this function manages player moves
def playerMove(move):
return
# this function manages computer moves
def computerMove(computerMove):
return
while True:
print(drawBoard())
move=input("Please choose a move (99 to quit): ")
if move == "99":
break
elif str(move) in computerSpaces or str(move) in humanSpaces:
print("this is a invalid move")
else:
humanSpaces.append(move)
print(humanSpaces)
# playerMove(move)
Javascript Version[edit]
How you will be assessed[edit]
Your solution will be graded using the following axis:
Scope
- To what extent does your code implement the features required by our specification?
- To what extent is there evidence of effort?
Correctness
- To what extent did your code meet specifications?
- To what extent did your code meet unit tests?
- To what extent is your code free of bugs?
Design
- To what extent is your code written well (i.e. clearly, efficiently, elegantly, and/or logically)?
- To what extent is your code eliminating repetition?
- To what extent is your code using functions appropriately?
Style
- To what extent is your code readable?
- To what extent is your code commented?
- To what extent are your variables well named?
- To what extent do you adhere to style guide?
References[edit]
A possible solution[edit]
Click the expand link to see one possible solution in Python, but NOT before you have tried and failed!
import os, random, time
#setting
table = ["\033[1;30m1\033[0m","\033[1;30m2\033[0m","\033[1;30m3\033[0m","\033[1;30m4\033[0m","\033[1;30m5\033[0m","\033[1;30m6\033[0m","\033[1;30m7\033[0m","\033[1;30m8\033[0m","\033[1;30m9\033[0m"]
# table1 = ["\033[1;30m1\033[0m","\033[1;30m2\033[0m","\033[1;30m3\033[0m","\033[1;30m4\033[0m","\033[1;30m5\033[0m","\033[1;30m6\033[0m","\033[1;30m7\033[0m","\033[1;30m8\033[0m","\033[1;30m9\033[0m"]
ending = False
def drawing():
os.system('clear')
print(f"{table[0]} {table[1]} {table[2]}")
print(f"\n{table[3]} {table[4]} {table[5]}")
print(f"\n{table[6]} {table[7]} {table[8]}")
#event loop
while True:
drawing()
a=0
b=0
#win or lose or draw
# if any(table1) in table:
# print("You draw!")
# ending = True
if table[0] == table[4] == table[8]:
if table[0] == 'O':
print("You Win!")
ending = True
else:
print("You Lose!")
ending = True
elif table[2] == table[4] == table[6]:
if table[2] == 'O':
print("You Win!")
ending = True
else:
print("You Lose!")
ending = True
for i in range(3):
if table[a] == table[a+1] == table[a+2]:
if table[a] == 'O':
print("You Win!")
ending = True
else:
print("You Lose!")
ending = True
elif table[b] == table[b+3] == table[b+6]:
if table[b] == 'O':
print("You Win!")
ending = True
else:
print("You Lose!")
ending = True
a += 3
b += 1
if ending == True:
break
#user's turn
user_move = int(input("\nplace number: "))
if table[user_move-1] != 'O' and table[user_move-1] != 'X':
table[user_move-1] = 'O'
else:
input("that's not proper place...")
continue
drawing()
#computer's turn
while True:
a=0
b=0
## simple UI logic
# if table[4] == 'X' or 'O':
# if table[4] == table[0] or table[4] == table[8]:
# if table[0] == 'O' or 'X':
# table[8] = 'X'
# break
# elif table[8] == 'O' or 'X':
# table[0] = 'X'
# break
# elif table[4] == table[2] or table[4] == table[6]:
# if table[2] == 'O' or 'X':
# table[6] == 'X'
# break
# elif table[6] == 'O' or 'X':
# table[2] == 'X'
# break
# else:
# pass
# for i in range(3):
# if table[a] == table[a+1] or table[a+1] == table[a+2] and table[a+1] == 'X' or 'O':
# if table[a] == 'O' or 'X':
# table[a] = 'X'
# logic_loop = False
# if table[a+2] == 'O' or 'X':
# table[a+2] = 'X'
# break
# elif table[b] == table[b+3] or table[b+3] == table[b+6] and table[b+3] == 'X' or 'O':
# if table[b] == 'O' or 'X':
# table[b] = 'X'
# break
# if table[b+6] == 'O' or 'X':
# table[b+6] = 'X'
# break
# else:
# pass
# a += 3
# b += 1
com_move = random.randrange(1,10)
if table[com_move-1] != 'O' and table[com_move-1] != 'X':
table[com_move-1] = 'X'
break
else:
continue
time.sleep(0.7)
# ============================ second example below ====================
import random
# initialization stuff here:
board = [1,2,3,4,5,6,7,8,9]
computerSpaces = []
humanSpaces =[]
game = 0
# this function draws pieces on the board.
def drawPieces(position):
if position in humanSpaces:
return "X"
elif position in computerSpaces:
return "O"
else:
return position
# this function manages player moves
def playerMove(move):
humanSpaces.append(move)
humanSpaces.sort()
board.remove(int(move))
return
# this function manages computer moves
def computerMove():
# we choose a possible move from any moves left in the list board
move=random.choice(board)
# we append the move we just made into a list
computerSpaces.append(str(move))
# we sort the list because our win conditions are in a certain order. 1,2,3 is a win, but 3,2,1 isn't.
# sorting is a bit of a hack (there might be a better way to test for membership in a list iregardless of order)
computerSpaces.sort()
# finally, we remove the place from our list keeping track of the places.
board.remove(move)
return
# this function draws the board
def drawBoard():
print("")
print("")
print(" ",drawPieces('1')," | ",drawPieces('2')," | ",drawPieces('3'), " ")
print(" | | ")
print("----------------------")
print(" ",drawPieces('4')," | ",drawPieces('5')," | ",drawPieces('6'), " ")
print(" | | ")
print("----------------------")
print(" ",drawPieces('7')," | ",drawPieces('8')," | ",drawPieces('9'), " ")
print(" | | ")
return
# this functions looks for win win conditions
def winConditions():
winConditions = [['1','2','3'],['4','5','6'],['7','8','9'],['1','5','9'],['3','5','7'],['1','4','7'],['2','5','8'],['3','6','9']]
if any(humanSpaces) in winConditions:
print("human won")
raise SystemExit
elif any(computerSpaces) in winConditions:
print("commputer won")
raise SystemExit
return
# main game loop
while True:
print(board)
print(drawBoard())
move=input("Please choose a move (99 to quit): ")
if move == "99":
break
elif str(move) in computerSpaces or str(move) in humanSpaces:
print("this is a invalid move")
else:
playerMove(move)
computerMove()
winConditions()
print(humanSpaces)
print(computerSpaces)
# playerMove(move)
Click the expand link to see one possible solution in Javascript, but NOT before you have tried and failed!