Project [6]: Functions Solution

$35.00 $29.05

You'll get a: . zip file solution, download link after Payment

Description

Project Goals:

The goals of this project are to:

  • Get students familiar with the use of functions

  • Show students how simple it can be to implement complicated-looking functions.

Important Notes:

  1. Formatting: Make sure that you follow the precise recommendations for the output content and

formatting: for example, do not change the text of the problem from “Player 1 enter your selection

[row, col]: to Player 1 selection: ”. Your assignment will be auto-graded and any change in formatting will result in a loss in the grade.

  1. Comments: Header comments are required on all files, for each function, and recommended throughout the rest of the program. Points will be deducted if no header/function comments are included.

  1. Restriction: The use of goto statements anywhere within this program is prohibited. Points will be deducted if goto is used.

Problem 1

Write a program that implements the game of Tic-Tac-Toe where you can play against the computer. Player 1 will be the user and player 2 will be the computer. Your program should go through the following steps:

  1. Generate an empty Tic-Tac-Toe board (3×3 array)

  1. Run a loop until one of the players places three in a row (a player has won) or the table is full (stalemate). This loop should:

    1. Display the current layout of the table

      1. Blank spaces are displayed as an underscore

      1. O for player 1’s moves (user)

      1. X for player 2’s moves (computer)

      1. Put spaces between the squares

    1. If it is player 1’s turn (the user)

      1. Ask the user to enter their selection (the location on the board where the O should be placed (row, col))

      1. Check to make sure that the row and column the user entered is valid

      1. If the row or column player 1 entered was invalid (outside the bounds of the board or a space that is already occupied) the program should ask the user to enter the option again.

    1. If it is player 2’s turn (the computer)

      1. Randomly generate a move (row, col) in the board that is not currently occupied (if the computer selects and occupied space generate a new move)

      1. To generate a random move: generate two random integers, one for the row and one for the column, each between 0 and 2

      1. Print out player 2’s move

    1. Update the board with player 2’s move

  1. If the above loop ends because one of the players has placed three in a row then the program should print out a winning message of that player (see below). Else, it should print out the follow: “Game over, no player wins.

The program should function as follows (items underlined are to be entered by the user):

The current state of the game is:

_ _ _

_ _ _

_ _ _

Player 1 enter your selection [row, col]: 1,2

The current state of the game is:

_ O _

_ _ _

_ _ _

Player 2 has entered [row, col]: 1,3

The current state of the game is:

_ O X

_ _ _

_ _ _

Player 1 enter your selection [row, col]: 2,2

The current state of the game is:

_ O X

_ O _

_ _ _

Player 2 has entered [row, col]: 2,1

The current state of the game is:

_ O X

X O _

_ _ _

Player 1 enter your selection [row, col]: 3,2

The current state of the game is:

_ O X

X O _

_ O _

Congratulations, Player 1 wins!

Your program should implement and use the following functions:

  • Function name: display_table

    1. Return:

      • Nothing

    1. Parameters:

      • The board as a 3×3 array

    1. Requirements:

      • Prints out the following message:

        • The current state of the game is:

      • Prints out the current status of the board (as shown above)

        • Print out an underscore ‘_’ for an empty cell

  • Function name: clear_table

    1. Return:

      • Nothing

    1. Parameters:

      • The board as a 3×3 array

    1. Requirements:

      • Clear the board for the beginning of the game by making every position in the array an empty cell

  • Function name: check_table_full

    1. Return:

      • True if the board is full

      • False if the board is not full

    1. Parameters:

      • The board as a 3×3 array

    1. Requirements:

      • Checks to see if the board is full or not

  • Function name: update_table

    1. Return:

      • Nothing

    1. Parameters:

      • The board as 3×3 array

      • The move (row and column)

      • The players token (either an X or an O)

    1. Requirements:

      • Updates the board with the given player token (either an X or an O) at the given move (row and column)

  • Function name: check_legal_option

    1. Return:

      • True if the given move is legal

      • False if the given move is illegal

    1. Parameters:

      • The board as a 3×3 array

      • The possible move (row and column)

    1. Requirements:

      • Checks to see if the given move is within the bounds of the board

      • Checks to see if the given move is on an empty cell

  • Function name: generate_player2_move

    1. Return:

      • Nothing

    1. Parameters:

      • The board as a 3×3 array

      • The move (row and column)

    1. Requirements:

      • If the game is not over:

        • Generate a valid, random move for player 2

        • Update the board with the generated move

        • Print out the generated move (as seen above)

        • Print out the current state of the board

  • Function name: check_three_in_a_row

    1. Return:

      • Zero if no one has three in a row

  • One if player 1 has three in a row

  • Two if player 2 has three in a row o Parameters:

  • The board as a 3×3 array

    1. Requirements:

      • Returns the ID of the player that has three in a row or zero if no one has three in a row

  • Function name: check_end_of_game

    1. Return:

      • True if the game has ended

      • False if the game hasn’t ended

    1. Parameters:

      • The board as a 3×3 array

    1. Requirements:

      • Returns true or false depending on if the game is over or not

  • Function name: get_player1_move

    1. Return:

      • Nothing

    1. Parameters:

      • The board as a 3×3 array

      • The move (row and column)

    1. Requirements:

      • If the game is not over:

        • Get a possible move from the user (as seen above)

        • If the given move is not valid get another move from the user until you have a valid move

        • Update the board with the given, valid move

        • Print out the current state of the board

  • Function name: print_winner

    1. Return:

      • Nothing

  1. Parameters:

    • The board as 3×3 array o Requirements:

    • If a player has won prints out the victory message (as seen above)

    • If the game is a stalemate prints:

      • Game over, no player wins.

This is the main function. Copy this into your code and write the ten functions from above in order to make the program work.

int main ()

{

//Declare the tic-tac-toe board

char board[SIZE][SIZE];

//The row and column of the move for either player 1 or 2

int row, col;

//Clear the table

clear_table(board);

//Display the table

display_table(board);

do

{

//Have player 1 enter their move

get_player1_move(board, row, col);

//Generate player 2 move

generate_player2_move(board, row, col);

//Do this while the game hasn’t ended

}while(check_end_of_game(board) == false);

//After the game is over, print who won

print_winner(board);

return 0;

}

Notes:

  • You are NOT allowed to use global variables

  • You are NOT allowed to change the main function

  • You cannot add parameters to any function other than the ones described above

  • SIZE is defined as 3 using a #define at the top of the program

Save your program as tictactoe.c

Challenge for problem 1 (10 extra credit points):

Make your program run in a loop. At the end of the game your program should print “Would you like to play again (Y/N): ”. The user will then enter either a Y indicating they do want to play again or an N indicating they do not want to play again and the program should end. If the user does want to play again the board should be reset and a new game started.

Note:

  • You may change the main function for the challenge Save your challenge separately as tictactoe_c.c

Grading Rubric

Grading will be done for each problem as follows:

Correctly-named file

5%

Header comment

2%

Program compiles

5%

Correctly-reading data from terminal

28%

Correct result printed

60%

Submission details

To submit your project, you will have to use the submission script. You do this by either:

  1. Working on an ECC machine

  1. Working on the provided VMware

  1. Secure Copying your files (See Mac Support for information)

To Submit your project:

  • Have a directory called “project6”

  • Save your *.c files in that directory

  • To submit: (don’t type the ‘>’ symbols)

    • cd project6

    • submit

The submission script copies all files in the current directory to our directory. You may submit as many times as you like before the deadline, we only keep the last submission.

Academic Honesty

Academic dishonesty is against university as well as the system community standards. Academic dishonesty includes, but is not limited to, the following:

Plagiarism: defined as submitting the language, ideas, thoughts or work of another as one’s own; or assisting in the act of plagiarism by allowing one’s work to be used in this fashion.

Cheating: defined as (1) obtaining or providing unauthorized information during an examination through verbal, visual or unauthorized use of books, notes, text and other materials; (2) obtaining or providing information concerning all or part of an examination prior to that examination; (3) taking an examination for another student, or arranging for another person to take an exam in one’s place; (4) altering or changing test answers after submittal for grading, grades after grades have been awarded, or other academic records once these are official.

Cheating, plagiarism or otherwise obtaining grades under false pretenses” constitute academic dishonesty according to the code of this university. Academic dishonesty will not be tolerated and

penalties can include cancelling a student’s enrolment without a grade, giving an F for the course, or for the assignment. For more details, see the University of Nevada, Reno General Catalog.