Docsity

Tic Tac Toe Submitting your project, Study notes of Computer Programming

Toggle sidebar
bg1
COSC 117 Spring 2018 – Programming Project 3 Page 1 of 5
Tic Tac Toe
For this project, you will write a program that plays the game Tic Tac Toe.
Overview
The program will welcome the user and ask if they would like to play a game. If they answer
yes, the program will ask the user who should go first, the computer or the human. Taking this
into account the program will alternate computer and human player moves until one of three
situations occur:
1. There are three ‘X’s in a row on the board. In this case, the computer wins since for our
program, the computer always plays X.
2. There are three ‘O’s in a row on the board. In this case, the human wins since for our
program, the human always plays O.
3. All of the positions have an ‘X’ or an ‘O’, but the same symbol does not occur three in a
row on the board. This is a tie.
Once one of these situations occurs, the game is over and the program displays the result. The
program will then ask the user if they want to play again. If they answer yes, the game process
starts anew. If they answer no, the program bids farewell to the user and ends.
Writing the Program
A program skeleton (TicTacToeStarter.java) has been provided to help you write your program.
The skeleton includes two class variables that are visible to all of the program’s methods: a
Scanner used to read user input from the keyboard and a char array of size nine used to represent
the tic tac toe board.
In all, the program consists of eleven methods: main, playGame,
initializeGameBoard, processComputerMove, processHumanMove,
displayGameBoard, isDone, isComputerWin, isHumanWin, isPlayerWin, and
isTie. A few of these already have some code in them, but most don’t. All of them contain
instructions on what you need to do in these methods.
Your job is to complete all of the missing code. In some cases you can write the code as you
want, but in others you are provided with some information about what you need to do.
Ultimately, your program should work in a fashion similar to what is presented in the Sample
Run sections below. For your implementation, you should rename the file TicTacToeStarter.java
to TicTacToe<YourLastName>.java.
Submitting your project
The project is due at the beginning of lab on Tuesday, ????? ??th. in lab.
At that time you need to hand in printouts of the following (stapled together):
1. Your source code for TicTacToe.java
pf3
pf4
pf5

Partial preview of the text

Download Tic Tac Toe Submitting your project and more Study notes Computer Programming in PDF only on Docsity!

Tic Tac Toe

For this project, you will write a program that plays the game Tic Tac Toe.

Overview

The program will welcome the user and ask if they would like to play a game. If they answer

yes, the program will ask the user who should go first, the computer or the human. Taking this

into account the program will alternate computer and human player moves until one of three

situations occur:

1. There are three ‘X’s in a row on the board. In this case, the computer wins since for our

program, the computer always plays X.

2. There are three ‘O’s in a row on the board. In this case, the human wins since for our

program, the human always plays O.

3. All of the positions have an ‘X’ or an ‘O’, but the same symbol does not occur three in a

row on the board. This is a tie.

Once one of these situations occurs, the game is over and the program displays the result. The

program will then ask the user if they want to play again. If they answer yes, the game process

starts anew. If they answer no, the program bids farewell to the user and ends.

Writing the Program

A program skeleton (TicTacToeStarter.java) has been provided to help you write your program.

The skeleton includes two class variables that are visible to all of the program’s methods: a

Scanner used to read user input from the keyboard and a char array of size nine used to represent

the tic tac toe board.

In all, the program consists of eleven methods: main, playGame,

initializeGameBoard, processComputerMove, processHumanMove,

displayGameBoard, isDone, isComputerWin, isHumanWin, isPlayerWin, and

isTie. A few of these already have some code in them, but most don’t. All of them contain

instructions on what you need to do in these methods.

Your job is to complete all of the missing code. In some cases you can write the code as you

want, but in others you are provided with some information about what you need to do.

Ultimately, your program should work in a fashion similar to what is presented in the Sample

Run sections below. For your implementation, you should rename the file TicTacToeStarter.java

to TicTacToe< YourLastName >.java.

Submitting your project

The project is due at the beginning of lab on Tuesday, ????? ??th. in lab.

At that time you need to hand in printouts of the following (stapled together):

1. Your source code for TicTacToe.java

2. Output from running your program. Your output should demonstrate the various features

of your program. Your output example should include as many executions as necessary

to demonstrate the various conditions in your program.

Also, you need to hand in a USB flash drive that contains your TicTacToe< lastname > project

directory. Do not provide your entire workspace… just the TicTacToe project directory. Clearly

label your flash drive with your name.

At that time you should hand in a printout of your TicTacToe< YourLastName >.java file and a

copy ofalso must email your code to me by the above time at [email protected]

Sample Run 1

Welcome to Tic-Tac-Toe!! Would you like to play a game? (enter 'yes' or 'no'): yes Who should move first? (c=computer or h=human): c The computer is X, the human is O. The computer chooses cell 6. | | ---+---+--- | | ---+---+--- X | | Enter an empty position number (0-8): 0 The computer chooses cell 3. O | | ---+---+--- X | | ---+---+--- X | | Enter an empty position number (0-8): 4 The computer chooses cell 5. O | | ---+---+--- X | O | X ---+---+--- X | | Enter an empty position number (0-8): 8 The human wins! The human must have cheated. O | | ---+---+--- X | O | X ---+---+--- X | | O

X | | X

Enter an empty position number (0-8): 3 The computer chooses cell 5. O | | O ---+---+--- O | | X ---+---+--- X | | X Enter an empty position number (0-8): 4 The computer chooses cell 7. The computer wins! Humans are not very bright. O | | O ---+---+--- O | O | X ---+---+--- X | X | X Game over! Would you like to play again? (enter 'yes' or 'no'): no Goodbye!

Sample Run 3

Welcome to Tic-Tac-Toe!! Would you like to play a game? (enter 'yes' or 'no'): maybe Goodbye!

Sample Run 4

Welcome to Tic-Tac-Toe!! Would you like to play a game? (enter 'yes' or 'no'): yes Who should move first? (c=computer h=human): c The computer is X, the human is O. The computer chooses cell 3. | | ---+---+--- X | | ---+---+--- | | Enter an empty position number (0-8): 4 The computer chooses cell 0. X | | ---+---+--- X | O |

Enter an empty position number (0-8): 6 The computer chooses cell 2. X | | X ---+---+--- X | O | ---+---+--- O | | Enter an empty position number (0-8): 1 The computer chooses cell 8. X | O | X ---+---+--- X | O | ---+---+--- O | | X Enter an empty position number (0-8): 5 The computer chooses cell 7. A tie! The human got lucky. X | O | X ---+---+--- X | O | O ---+---+--- O | X | X Game over! Would you like to play again? (enter 'yes' or 'no'): no Goodbye!