Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ITI 1121. Introduction to Computing II   Winter 2022

Assignment 2

Learning objectives

•  Using Interfaces

•  Polymorphism

Important note: Your assignment will not be graded and will get zero if:

-      Names are missing at the beginning of all java files.

-     Your solution files are not submitted in the correct format (see the last page).

Introduction

In this assignment, we are continuing our work on the Tic-Tac-Toe game. As part of our previous assignment, we designed and implemented a basic game that can be played by two people. We'll create a "computer player" this time. While it isn't very smart, it at least follows the rules if it follows them. We will thus be able to play human against computer.

Before you proceed with implementing the specific requirements of this assignment, you need to generalize the winning strategy to any number of X's or O's. If youfailed to implement assignment 1for the specific case of 3, a sample solution for that case will be announced after a week of the publish date of this assignment.

Human vs. (Dumb) Machine

A very simple way to have a program play Tic-Tac-Toe is to simply have the program pick an empty cell randomly to play at each turn. Of course, such an implementation should be easy to beat, but at least it can be played against. To design this solution, we want to introduce the concept of a Player. For now, we will have two kindsof players:

the human Player and the dumb computer player.  Later, we can introduce more types of players, e.g.,  asmart computer player, a perfect player, etc. All of these are Players.

 

Figure 1: The interface Player and the two classes implementing it.

What we gain from this Players abstraction is that it is possible to organize a match between two players and have these two players play a series of games, keeping score for the match, etc., without having to worry about the type of players involved. We can have human vs. human, human vs. dumb computer, smart vs dumb computer players, or any combination of players; this does not impact the way the game is played: we have two players, and they alternate playing a move on the game until the game is over. The requirement to be able to do this is that all

Players implement the same method, say play(), which can be called when it is that Player's turn to play. 1

In our current implementation in assignment2, we always play a human against a computer. The human is player1, and the computer is player2The Player who playsfirst initially is chosen randomly. In subsequent

games, the players alternate as the first Player. As usual, the first Player plays X and the second Player plays O so each Player will alternate betweenplaying X and playing O.

The following printout shows a typical game.

$ java TicTacToe

************************************************************

*

*

*

*

*

*

*

*

************************************************************

Player 2's turn.

Player 1's turn.

||

X||

|      |

O  to  play:

Here, player 2 (the computer) was selected to start the first game. As can be seen, the computer player doesn't print out anything when it plays; it just makes its move silently. Then, it is player 1's turn (human). Following whatwe did in assignment 1, the HumanPlayer object first prints the game (here, we can see that the computer playedcell 4) and then prompts the actual human (us, the user) for a move. Below, we see that the human has selected cell

1. The computer will then play (silently), and the human will be prompt again. It continues until the game finishes:

O to play: 1

Player 2's turn.

Player  1's t u r n .

O |     | X

X |      |

|      |

O to play: 5

Player 2's turn.

Player  1's turn.

O |     | X

X |  O |

|      | X

O to play: 6

Player 2's turn.

Player  1's turn.

O |  X | X

X |  O | O

|      | X

O  to  play:  7

Player 2's turn.

Game over

O  | X  | X 

X|O| O

O  | X  | X

Result: DRAW

Play  again  (Y)?:

This game finishes with a DRAW. The sentence "Game over" is printed after the last move  (made by the computer in this case), then the final board is printed, and the outcome of the game ("Result: DRAW").

The user is then asked if they want to play again. Check the following example where we continue with "y".

Play again  (Y)?:y

Player  1's turn.

||

||

|      |

X to play: 5

Player 2's turn.

Player  1's turn.

||

|X|

O  |     |

X to play: 1

Player 2's turn.

Player 1's turn.

X||

|X|O

O |     |

X  to  play:  9

Game over

X||

|X|O

O  |     | X

Result: XWIN

Play again  (Y)?:y

Player 2's turn.

Player  1's turn.

|      |

|      |

X  |     |

O to play: Player 2's Player  1's

O |     |

1

turn.

turn.

|      |

X |  X  |

O to play: 9

Player 2's turn.

Player  1's turn.

O |     |

|

| X

X |  X  | O

 

O  |     |

| O  | X

X  | X  | O

Result: OWIN

Play again  (Y)?:n

$

Implementation

We are now ready to program our solution. We will reuse the implementation of the class TicTacToeGame from assignment 1. A class Utils has been provided to get simple access to a few constants and global variables.

•   Player

Player is an interface.  It defines only one method, the method play.  Play is void and has one input parameter, a reference to a TicTacToeGame.

•   HumanPlayer

HumanPlayer is a class that implements the interface Player. In its implementation of the method play, it first checks that the game is indeed playable (and prints out an error message ifthat is not the case), and then queries the user for valid input, reusing the code that was in the main of the class TicTacToe ofassignment 1. Once suchan input has been provided, it plays in on the game and returns.

•   ComputerRandomPlayer

ComputerRandomPlayer is a class that also implements the interface Player. In its implementation of the method play, it first checks that the game is indeed playable  (and prints out an error message if that is not the case), andthen chooses the next move randomly and plays it on the game, and returns. All the possible next moves have anequal chance of being played.

•   TicTacToe

This  class implements playing the game. You are provided with the initial part very similar to the  one from assignment 1. The entire game is played in the main method. A local variable player, a reference to an array of two players,  is  used to  store the  human  and the  computer player. You  must  use that  array to  store your  Player references.

You need to finish the implementation of the main to obtain the specified behavior. You need to ensure that the  first  Player  is  initially  chosen  randomly  and  that  the  first  move  alternate  between  both  players  in subsequentgames. A simple algorithm is provided for you in the given files.

Below is another sample run, this time on a 4x4 grid with a win length of 2. The human players make a series of input mistakes along the way.

$ java TicTacToe 4 4 2

************************************************************

*

*

*

*

*

*

*

*

************************************************************

Player 1's turn.

|||

|||

|||

|      |      |

X to play: 2

Player 2's turn.

Player  1's  turn.

|X||

||O|

|||

|      |      |

X to  play: 2

This cell  has already  been  played

|X||

||O|

|||

|      |      |

X to play:  -1

The value should  be  between  1 and  16

|X||

||O|

|||

|     |     |

X to play:  3

Game over

|X|X|

||O|

|||

|     |     |

Result: XWIN

Play again  (Y)?:y

Player 2's turn.

Player  1's turn.

|||

|||

||X|

|      |      |

O  to  play:  11

This cell  has already  been  played

|      |      |

|      |      |

|      | X  |

|      |      |

O to play: 12

Player 2's turn.

Player  1's  turn.

|      | X  |

|

|      |

|

| X  | O

|      |      |

O to play: 13     Player 2's turn.

Game over

|      | X  |

|      |      |

|      | X  | O

O  | X  |     |

Result: XWIN

Play again  (Y)?:n

$

Academic Integrity

This part of the assignment is meant to raise awareness concerning plagiarism andacademic integrity. Please read the following documents.

https://www.uottawa.ca/vice-president-academic/academic-integrity

https://www2.uottawa.ca/about-us/policies-regulations/academic-regulation-i-14-academic-fraud

Cases of plagiarism will be dealt with according to the university regulations. Bysubmitting this assignment, you acknowledge:

1. I have read the academic regulations regarding academic fraud.

2. I understand the consequences of plagiarism.

3. With the exception of the source code provided by the instructors for this course, allthe source code is mine.

4. I did not collaborate with any other person, with the exception of my partner in the case ofteamwork.

• If you did collaborate with others or obtained source code from the Web, please list the names ofyour collaborators or the source of the information and the nature ofthe               collaboration. Put this information in the submitted README.txt file. Markswill be         deducted proportionally to the level of help provided (from 0to 100%).

Rules and regulation

•      Submit your assignment through the online submission system at brightspace

•      You must preferably do the assignment in teams of two, but you can also do the assignment individually.

•      If you do not follow the instructions, your program will make the automatedtests fail, and consequently, your assignment will not be graded.

•      We will be using an automated tool to compare all the assignments against each other (this includes both the French and English sections). Submissions that are flagged by this tool will receive a grade of 0.

•      It is your responsibility to make sure that BrightSpace has received your assignment. Late submissions will not be graded.

Files

You must hand in a zip file (no other file format will be accepted). The name of the top directory has to have the following form: a2_3000000_3000001, where 3000000 and 3000001 are the student numbers of the team members

submitting the assignment (simply repeat the same number ifyour team has one member). The name of the folder starts with the letter "a"  (lowercase), followed by the number of the assignment, here 2. The parts are

separated by the underscore (not the hyphen). There are no spaces in the name ofthe directory.

Your submission must contain thefollowing files.

•  README.txt

–  A text file that contains the names of the two partners for the assignments, their student ids, section, and a short description of the assignment (one or two lines).

–  CellValue.java

–  ComputerRandomPlayer.java

–  GameState.java

–  HumanPlayer.java

–  Player.java

–  StudentInfo.java

–  TicTacToe.java

–  TicTacToeGame.java

–  Utils.java