Java代写:CS130 Tic-Tac-Toe

Introduction

实现一个Tic-Tac-Toe的游戏,Tic-Tac-Toe是游戏里面最基础的游戏之一,每次落子判断横竖斜一共四个方向的同色棋子即可。

Background

This project is the rst in a series of three, with the ultimate objective of designing and implementing (in Java) the game of Tic Tac Toe. It is a two player game, and the rules are as follows:

  1. The game begins with an empty, 3 _ 3 grid.
  2. The two players then take turns placing a mark in an empty grid cell. Player O will use the ‘O’ (letter ‘O’, not zero) mark and Player X will use the ‘X’ mark.
  3. The game is over in either case: (i) one player has 3 marks in a sequence (vertically, horizontally, diagonally, or anti-diagonally), or (ii) there are no empty cells left.

Below we show a run of the game, where Player O moved rst and won the game by placing 3 ‘O’s in the second row of the game grid.

Your Task

For this rst project, your task is to write a class named TicTacToe that will enable two players to play a single game of Tic-Tac-Toe. To achieve this goal you will practise the use of instance variables and methods through the following stages.

Initialise the Game

Your TicTacToe class should have a main method. This method will create an object of the TicTacToe class named game and call a method run to run a game:

1
2
TicTacToe game = new TicTacToe();
game.run();

Here the main method will only serve as a testing method for the TicTacToe class. Rather than creating it in a separate TicTacToeTest class, we put it in the TicTacToe class to simplify the project.
The game running process will be handled in the run method, which you need to implement. Note that you should make run an instance method of the TicTacToe class, not a static method. The same applies for the methods described in the following sections.
The run method will start with printing out a welcome message and prompting for the two players’ names (note: you should type in the text yourself instead of copying from this PDF because copying from PDF may bring non-English characters into your program which may fail the compilation at submission):

Welcome to Tic Tac Toe!
Enter Player O's name:
Rose
Enter Player X's name:
Jack

Note here Rose and Jack are user input, not part of the program output. You should add proper instance (non-static) variables to TicTacToe to store the two players’ names, as well as accessors and mutators for them. You may assume that there is no space character in the player names.

Next, the game starts. The program will print out the game grid.
You need to add a printGrid method to the TicTacToe class for printing out the game grid. You should also add proper instance variables to keep track of what mark has been placed at each grid cell. For the 9 grid cells, you will need 9 instance variables (you can use an array to simplify this if you know how to use it properly, but you are not required to do so in this project). At start the grid is empty. You should initialise the instance variables properly to reach this. Your printGrid method will access these instance variables to determine what to print at each grid cell.
The run method will then prompt for player’s move. We assume that Player O always moves rest.

Rose's move:
1 1

Here a player’s move is represented by two integers in the range of “0 0” to “2 2”, where “0 0” represents the top-left cell and “2 2” represents the bottom-right cell. You need to add a proper method to update the instance variables that store the grid cell status after a user has make a move.

Loop through the Game

Now add a loop to repeatedly prompt for the players to take turns and make moves. The game grid should be printed out after each move. Here is an example execution after adding the loop.
Tests will be conducted on your program by automatically compiling, running, and comparing your outputs for several test cases with generated expected outputs. These test cases used will be different from the sample test given. The automatic test will deem your output wrong if your output does not match the expected output, even if the difference is just having an extra space. Therefore, it is crucial that you generate your own test case and test your program extensively.
The syntax import is available for you to use standard Java packages. However, please DO NOT use the package syntax to customize your source codes. The automatic test system cannot deal with customized packages. If you are using Netbeans as the IDE, please be aware that the project name may automatically be used as the package name. Please remove any lines of the form package ProjA.
At the beginning of the source codes before you submit them to the system.
Please use ONLY ONE Scanner object throughout your program. Otherwise the automatic test will cause your program to generate exceptions and terminate. The reason is that in the automatic test, multiple lines of test inputs are sent all together to the program. As the program receives the inputs, it will pass them all to the currently active Scanner object, leaving any remaining Scanner objects with nothing to read, causing a run-time exception. Therefore it is crucial that your program has only one Scanner object. Arguments such as it runs correctly when I do manual test, but fails under automatic test” will not be accepted.