You are on page 1of 7

Programming Language II 111-2

Assignment 7
Requirements:
● Create a Java project named StudentId_Assignment7
● Read instructions and create classes needed.
● All instance variables are private. Please use public methods to access private instance variables.

Description:
Now, you are a game interface programmer, you need to implement an OOXX game.
The game center has been done, so you just need to design the interface and call the game function to let it
work. You should watch the video in the link below to understand all the requirements for this assignment.

https://www.youtube.com/watch?v=XKXMQjy9FgI

OXGameManager class
OXGameManager

Return type Method or Variable description

Methods (You don’t need to implement)

void initialize() Set the board to the initial state.

String play(int) When a player takes a step, it will do the record on that
position and return the player symbol.

boolean finish() It will return whether the game is finished or not.

int getScoreO() Get player O score.

int getScoreX() Get player X score.

Method (You should implement)

String checkWin() It will confirm if there is a winner. If not, it will return null.

The beginning state:

1
Programming Language II 111-2

2
Programming Language II 111-2

The main method in Assignment7 class

You need to draw the layout of the GUI and implement all the buttons.
When a player presses the board, the index button will be placed to “O” or “X”.
Every time there is a winner, the winner’s score will increase.
When you press the ReStart button, the game board will be reset.
When you press the Finish button, the game window will be closed.

Sample output #1 Sample output #2

Sample output #3 Sample output #4

Submission: Submit your project as “.zip file” via Moodle. No other submissions will be graded.

3
Programming Language II 111-2

Reminder: Please zip the whole project


Deadline: After one weeks

4
Programming Language II 111-2

class Assignment7
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Assignment7 {

static OXGameManager manager = new OXGameManager();


static JButton[] btns = new JButton[9];
static JLabel score = new JLabel(String.format("O: %d ; X: %d", 0, 0));
static JButton reStart = new JButton("ReStart");
static JButton finish = new JButton("Finish");

public static void main(String[] args) {

// Implement GUI Layout here //

score.setFont(new Font("Arial", Font.PLAIN, 40));


reStart.setFont(new Font("Arial", Font.PLAIN, 40));
finish.setFont(new Font("Arial", Font.PLAIN, 40));

// Implement reStart button here //

// Implement finish button here //

for (int i=0; i<9; i++) {


JButton btn = new JButton();
int index = i;
btn.setText(Integer.toString(i));
btn.setFont(new Font("Arial", Font.PLAIN, 50));

// Implement the checkerboard button //

btns[i] = btn;
}

f.add(score);
f.add(reStart);
f.add(finish);
for (int i=0; i<9; i++) { f.add(btns[i]); }
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

5
Programming Language II 111-2

class OXGameManager
public class OXGameManager {
private boolean state = true;
private boolean finish = false;
private int count = 0;
private int[] checkerboard = new int[9];
private String winner = null;
private int scoreO = 0;
private int scoreX = 0;

// Play the O, X on checkerboard


public String play(int index) {

count++;
if(state) {

state = false;
checkerboard[index] = 1;
return "O";
}else {

state = true;
checkerboard[index] = -1;
return "X";
}
}

// Return if the game is finish or not


public boolean finish() {

return count == 9 ? true : finish;


}

// Initialize the game


public void initialize() {
state = true;
finish = false;
count = 0;
checkerboard = new int[9];
winner = null;
}

// Get O score
public int getScoreO() {
return scoreO;
}
// Get X score
public int getScoreX() {
return scoreX;
}

// Checking who is the winner or null


public String checkWin() {

// All line on the board


int[][] lines = new int[][] {
{0, 1, 2},
{3, 4, 5},

6
Programming Language II 111-2

{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6}
};

// Implement an algorithm to find the winner here //


// check if there is a winner on one of the lines (above local variable) //
// e.g. if O wins winner = "O"; //

return winner;
}

You might also like