I have it running well and would just like any suggestions.
Note: Yes I am using global variables and would like to keep it that way, at least for now. The reason is, that I am using The Great Courses to learn computer-science concepts, and the instructor insisted on using Global Variables...since we haven't learned about objects and such (got all the way to functions and that's it)
#Objective: write a tic-tac-toe program
#Main variables
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
goal_score = int(input("What score would you like to play up to?"))
current_score = 0
turn = 0
round = 1
round_gone_up = bool
def InitializeGrid(board):
for i in range(3):
for j in range(3):
board[i][j] = "."
return board
def InitializeBoard():
#initialize game
#Initialize grid
InitializeGrid(board)
global turn
# Prints out user-turn
turn = 1
print("Turn: ", turn)
global round_gone_up
round_gone_up = False
def ContinueGame(current_score, goal_score):
#Return false if game should end, true if game not over
if (current_score >= goal_score):
return False
else:
return True
def IsValid(move):
#move length
if len(move) != 2:
return False
#move number
if (int(move[0]) < 1):
print("Start at index 1")
return False
#move must be character 'x' or 'o'
if (move[1] != "x") and (move[1] != "o"):
return False
#no probs, so return true
return True
def GetMove():
move = input("Enter move: ")
while not IsValid(move):
move = input("Invalid move! Enter a valid move: ")
return move
def PositionBoard():
position = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
#Indexes a board one-to-nine
for i in range(1, 10):
for j in range(3):
for k in range(3):
position[j][k] = i
i += 1
break
return position
def CheckIfMoveAlreadyMade(board, move):
space_already_taken = []
board2 = PositionBoard()
for i in range(3):
for j in range(3):
#If board-space is not blank
if (board[i][j] != "."):
#Store board-space in list, convert board-space to string
space_already_taken += str(board2[i][j])
#While move[0] in the list of board-space
while move[0] in space_already_taken:
#Get new input for move
move = input("Space already taken! Enter a valid move: ")
continue
return move
def UpdateBoard(board, move):
board1 = PositionBoard()
for i in range(3):
for j in range(3):
if (move[0] == str(board1[i][j])):
board[i][j] = move[1]
return board
def CheckThree(board):
#Check each row for three-in-a-row
for i in range(2, -1, -1):
for j in range(2, -1, -1):
if (board[i][j] != "."):
if (board[i][j] == board[i][j-1]) and (board[i][j] == board[i][j-2]):
return board[i][j]
#Check each column
for j in range(3):
for i in range(3):
if (board[i][j] != "."):
if (board[i][j] == board[i-1][j]) and (board[i][j] == board[i-2][j]):
return board[i][j]
#Check diagonally
if (board[1][1] != "."):
if (((board[1][1] == board[0][0]) and (board[0][0] == board[2][2])
or (board[1][1] == board[0][2]) and (board[0][2] == board[2][0]))):
return board[i][j]
def CheckScore():
global round_gone_up
if (CheckThree(board)):
global current_score
global round
#Increments score
current_score += 1
#Increments round
round += 1
#Returns boolean round_gone_up
round_gone_up = True
#Prints the winner
print(CheckThree(board), " wins:", " round ", round)
return round_gone_up
def DrawBoard(board):
print("\n\n\n")
print(" ---------------------------------")
for i in range(3):
#Draw each row
linetodraw = ""
for j in range(3):
linetodraw += " | " + board[i][j]
linetodraw += " |"
print(linetodraw)
print(" ---------------------------------")
def DoRound():
DrawBoard(board)
move = CheckIfMoveAlreadyMade(board, GetMove())
UpdateBoard(board, CheckIfMoveAlreadyMade(board, move))
CheckThree(board)
CheckScore()
global current_score
print("Current score:", current_score)
print("Round:", round,)
#Initialize game
InitializeBoard()
while ContinueGame(current_score, goal_score):
# Checks if round gone up
if (round_gone_up):
InitializeBoard()
DoRound()
turn += 1
print("Turn: ", turn)
if not ContinueGame(current_score, goal_score):
print(CheckThree(board), "wins!")