Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Good Morning, Can i have help with my python coursework? I need to write a tic tac toe game in python. Definitions already given. I

Good Morning,

Can i have help with my python coursework? I need to write a tic tac toe game in python. Definitions already given. I copy and paste my python template in the end. Thank you.

Overview:

Noughts and Crosses is a simple classic game for 2 players, played on a three-by-three grid, in which players take it in turns to place their mark, either a nought O or a cross X, in a cell of the grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. For example:

To start the game, the convention is that X plays first. For the program you are going to develop, a human player plays against the computer, and the human player goes first with the cross X.

Getting Started:

Start by download the two Python files play_game.py and noughtsandcrosses.py. The file play_game.py is the main game program and it is already complete and it imports the noughtsandcrosses.py module in order to play the game. Your task is to fully complete the module noughtsandcrosses.py.

As with the previous Coursework, a number of incomplete functions are given to you and you must use these as they are. You must not change their arguments or the way they return values.

Requirements:

The requirement for each function is detailed in template file noughtsandcrosses.py and also explained below:

def draw_board(board) (2%)

You are required to develop code to draw the noughts and crosses board, for example:

def welcome(board) (2%)

You are required to develop code to print the welcome message and display the board by calling draw_board(board). For example:

def initialise_board(board) (2%)

You are required to develop code to set all elements of the board to one space ' '

def get_player_move(board) (5%)

You are required to ask the user for the cell to put the X in, and return row and col

The player has to input a valid cell.

def choose_computer_move(board) (25%)

You are required to develop code to let the computer chose a cell to put a nought in and return row and col

def check_for_win(board, mark) (5%)

You are required to develop code to check if either the player or the computer has won return True if someone won, False otherwise

def check_for_draw(board) (4%)

You are required to develop cope to check if all cells are occupied return True if it is, False otherwise

def play_game(board) (25%)

You are required to develop code to play the game.

Start with a call to the initialise_board(board) function to set the board cells to all single spaces ' '

Then draw the board

Then in a loop, get the player move

Update and draw the board

Check if the player has won by calling check_for_win(board, mark), if so, return 1 for the score.

If not check for a draw by calling check_for_draw(board).

If drawn, return 0 for the score

If not, then call choose_computer_move(board) to choose a move for the computer

Update and draw the board

Check if the computer has won by calling check_for_win(board, mark),

If so, return -1 for the score

If not check for a draw by calling check_for_draw(board)

If drawn, return 0 for the score

Repeat the loop from (3)

def menu() (5%)

You are required to develop code to get user input of either '1', '2', '3' or 'q'. For example:

def load_scores() (10%)

You are required to develop code to load the leaderboard scores from the file 'leaderboard.txt'. The function has to return the scores in a Python dictionary, with the player names as key and the scores as values.

def save_score(score) (10%)

You are required to develop code to ask the player for their name and then save the current score to the file 'leaderboard.txt'

def display_leaderboard(leaders) (5%)

You are required to develop code to display the leaderboard scores passed in the Python dictionary parameter "leader"

Structure and Documentation

The structure of your code and documentation will be analysed and assessed.

This will be done using a static analysis tool called Pylint. This software checks the code in your program, ensures that it follows Python conventions and that all functions, classes and modules have been documented. You can read more about it here: https://www.pylint.org/.

Python has an official style guide named PEP8 (https://peps.python.org/pep-0008/), which is where most Python conventions and coding standards originate from.

Example checks that Pylint carries out to ensure that the PEP8 coding standard is followed include things such as:

checking line-code's length

checking if variable names are well-formed

checking if imported modules/functions are used

checking if variables/function parameters are used

It is a good idea to run these checks on your code at regular intervals and before submitting.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

This is the python code template , which i need to use, with this definitions.

import random

import os.path

import json

random.seed()

def draw_board(board):

# develop code to draw the board

pass

def welcome(board):

# prints the welcome message

# display the board by calling draw_board(board)

pass

def initialise_board(board):

# develop code to set all elements of the board to one space ' '

return board

def get_player_move(board):

# develop code to ask the user for the cell to put the X in,

# and return row and col

return row, col

def choose_computer_move(board):

# develop code to let the computer chose a cell to put a nought in

# and return row and col

return row, col

def check_for_win(board, mark):

# develop code to check if either the player or the computer has won

# return True if someone won, False otherwise

return False

def check_for_draw(board):

# develop cope to check if all cells are occupied

# return True if it is, False otherwise

return True

def play_game(board):

# develop code to play the game

# star with a call to the initialise_board(board) function to set

# the board cells to all single spaces ' '

# then draw the board

# then in a loop, get the player move, update and draw the board

# check if the player has won by calling check_for_win(board, mark),

# if so, return 1 for the score

# if not check for a draw by calling check_for_draw(board)

# if drawn, return 0 for the score

# if not, then call choose_computer_move(board)

# to choose a move for the computer

# update and draw the board

# check if the computer has won by calling check_for_win(board, mark),

# if so, return -1 for the score

# if not check for a draw by calling check_for_draw(board)

# if drawn, return 0 for the score

#repeat the loop

return 0

def menu():

# get user input of either '1', '2', '3' or 'q'

# 1 - Play the game

# 2 - Save score in file 'leaderboard.txt'

# 3 - Load and display the scores from the 'leaderboard.txt'

# q - End the program

return choice

def load_scores():

# develop code to load the leaderboard scores

# from the file 'leaderboard.txt'

# return the scores in a Python dictionary

# with the player names as key and the scores as values

# return the dictionary in leaders

return leaders

def save_score(score):

# develop code to ask the player for their name

# and then save the current score to the file 'leaderboard.txt'

return

def display_leaderboard(leaders):

# develop code to display the leaderboard scores

# passed in the Python dictionary parameter leader

pass

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions