Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my program. I need to add in it a save funtion called \"save\" that basi

ID: 656347 • Letter: H

Question

Here is my program. I need to add in it a save funtion called "save" that basiclly saves the game at any time the user enter the letter "s" for input instead of a number. Also I need to add a function called "load" that asks the user at the beginning if they would like to load a game. Here is an emaple for saving a game:

Please enter a number of rows: 6

and to load that game they will need to run the program again and see that for everytime the program starts

Which saved game would you like to load? Game.txt

Welcome back!

Here is my code and all I need to add is the saving and loading part of the game

# This function takes in rows that are at least 5 and returns it

def getRows():

numRow = int(input("Please enter a number of rows: "))

if numRow < 5:

a = True

while a:

newRows = int(input("Please enter a valid choice: "))

if newRows >= 5:

a = False

return newRows

else:

return numRow

# This function takes in columns that are at least 5 and returns it

def getColumns():

numColumn = int(input("Please enter a number of columns: "))

if numColumn <5:

a = True

while a:

newColumns = int(input("Please enter a valid choice: "))

if newColumns >= 5:

a = False

return newColumns

else:

return numColumn

# This function ask the user if he/she would want to play again and returns a boolean

def nextRound():

goAgain = input("Would you like to play again (y/n): ")

if goAgain != "y" and goAgain != "n":

a = True

while a:

goAgain = input("Please enter a valid choice: ")

if goAgain == "y" or goAgain == "n":

a = False

return goAgain

else:

return goAgain

# This function takes in the rows and columns and then creates and return a board

def gameBoard(rows,columns):

board = []

for i in range(0,rows):

board.append([])

for k in range(0,columns):

board[i].append(0)

return board

# This function converts the values in the board into "_", "x" and "o" and prints the board

def convertedBoard(board):

for i in board:

for k in i:

if k == 0:

print("_", end="")

if k == 1:

print("x", end="")

if k == 2:

print("o", end="")

print("")

# This function prints whos turn it is

def playerMove(turn):

if turn == 1:

print("")

print("Player 1's turn: ")

if turn == 2:

print("")

print("Player 2's turn: ")

# This function ask the player for a column and check if the choice is valid and returns that column if it is valid

def moveWhere(numRows):

moveColumn = int(input("Please enter a move: "))

if moveColumn > numRows or moveColumn < 1:

a = True

while a:

moveColumn = int(input("Please enter a valid choice: "))

if moveColumn <= numRows and moveColumn >= 1:

a = False

return moveColumn

else:

return moveColumn

# This function ask the player to pick another column if the column he chose prior was full and returns that column

def chooseMoveAgain(move,numColumns,board,turn):

convertedBoard(board)

playerMove(turn)

print("Column is full. Pick again")

move = int(input("Please enter a move: "))

if move > numColumns or move < 1:

a = True

while a:

move = int(input("Please enter a valid choice: "))

if move <= numColumns and move >= 1:

a = False

return move

else:

return move

# This function fills in the player token(x/o) on the column they chose and returns back the board and coordinates of the last move

def moveNow(numColumns,numRows,move,board,turn):

height = numRows-1

move -= 1

while height > -1 and board[height][move] != 0:

height -= 1

if height > -1:

board[height][move] = turn

coordHeight = height

height = -2

coordColumn = move

else:

move = chooseMoveAgain(move,numColumns,board,turn)

height = numRows-1

return moveNow(numColumns,numRows,move,board,turn)

return board,coordHeight,coordColumn

# This function checks if the game is a draw based on the presense of 0 and returns a boolean

def checkDraw(board):

for i in board:

for k in i:

if k == 0:

return False

return True

# This function swap turns and returns the new turn

def swapTurns(turn):

if turn == 1:

turn = 2

else:

turn = 1

return turn

# This function checks for a vertical win and returns a boolean

def checkVerWin(board,coordColumn):

repeatHold = 1

for row in range(0,len(board)-1):

if board[row][coordColumn] != 0 and board[row][coordColumn] == board[row+1 [coordColumn]:

repeatHold += 1

if repeatHold >= 4:

return True

else:

repeatHold = 1

return False

  

# This function checks for a horizontal win and returns a boolean

def checkHorWin(board,coordHeight):

repeatHold = 1

for col in range(0,len(board[0])-1):

if board[coordHeight][col] != 0 and board[coordHeight][col] == board[coordHeight][col+1]:

repeatHold += 1

if repeatHold >= 4:

return True

else:

repeatHold = 1

return False

# This function checks for a diagonal win and returns a boolean

def checkDiaWin(board):

for row in range(0,len(board)-3):

for col in range(0, len(board[0])-3):

if board[row][col] != 0 and board[row][col] == board[row+1][col+1] and board[row][col] == board[row+2][col+2] and board[row][col] == board[row+3][col+3]:

return True

for row in range(0,len(board)-3):

for col in range(3,len(board[0])-1):

if board[row][col] != 0 and board[row][col] == board[row+1][col-1] and board[row][col] == board[row+2][col-2] and board[row][col] == board[row+3][col-3]:

return True

return False

  

# This function runs the game entire game. Once the game is over, it will run another round if the users want to.

def runGame():

numRows = getRows()

numColumns = getColumns()

turn = 1

board = gameBoard(numRows,numColumns)

convertedBoard(board)

a = 10

# If someone wins/board is filled, the game is over

  while a > 5:

playerMove(turn)

move = moveWhere(numRows)

board,coordHeight,coordColumn = moveNow(numColumns,numRows,move,board,turn)

convertedBoard(board)

isVerWin = checkVerWin(board,coordColumn)

if isVerWin == True:

print("Player", turn,"wins!")

a = 1

isHorWin = checkHorWin(board,coordHeight)

if isHorWin == True:

print("Player", turn,"wins!")

a = 1

isDiaWin = checkDiaWin(board)

if isDiaWin == True:

print("Player", turn,"wins!")

a = 1

isDraw = checkDraw(board)

if isDraw == True:

print("It is a draw")

a = 1

turn = swapTurns(turn)

  

playAgain = nextRound()

  print("")

  if playAgain == "y":

return runGame()

  else:

print("Thank you for playing!")

def main():

print("Welcome to Connect Four!")

runGame()

main()

The indentation is not correct but I tried to line them up so it would be eaiser to see please use python 3 to complete that part of the code

Explanation / Answer

Note the global game_state variable. You need something like that to keep track of all the objects that define your game state and keep them together for easy serialization / deserialization. (It doesn't necessarily have to be global, but it's definitely easier, and a game state like this is one of the few cases where it actually makes sense to use globals).

Saving the game using this code will result in a savegame.json that looks like this:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote