/** * This program plays a version of Reversi. * * @author Thomas Waltz * CS1121 * Fall 2005 * December 9, 2005 * Recitation Section 1 * Lab Section 2 */ import java.awt.*; import javax.swing.*; import java.awt.event.*; /* *This class does everything - draws, checks for winners, and keeps score. */ public class Game extends JApplet implements ActionListener { private int size = 8; //EVEN NUMBERS ONLY! JButton [][] buttons = new JButton[size][size]; private int turnvalue = 0; private int blue_value = 2; private int red_value = 2; private int i; private int j; private JLabel player = new JLabel("RED'S ", SwingConstants.RIGHT); private JLabel turn = new JLabel(" Turn"); private JLabel bluescore = new JLabel("BLUE ", SwingConstants.RIGHT); private JLabel redscore = new JLabel("RED ", SwingConstants.RIGHT); private JLabel bluescoreval = new JLabel("" + blue_value); private JLabel redscoreval = new JLabel("" + red_value); private Container window; /** * This is the main method that runs the game and sets up the grid. There are no parameters. */ public void init() { //Set the screen layout window = getContentPane(); window.setLayout(new GridLayout(size+1, size)); //i = row //j = column for (i = 0; i < size; i += 1) { for (j = 0; j < size; j += 1) { //add buttons to the display //add this - j + "c,r" + i - inside the JButton() below for printouts on the buttons buttons[j][i] = new JButton(); window.add(buttons[j][i]); buttons[j][i].setBackground(Color.white); //add a listener for all buttons in the array buttons[j][i].addActionListener(this); //set the colors of the middle four buttons if (i == size/2 && j == size/2) { buttons[j][i].setBackground(Color.red); } else if (i == size/2-1 && j == size/2-1) { buttons[j][i].setBackground(Color.red); } else if (i == size/2-1 && j == size/2) { buttons[j][i].setBackground(Color.blue); } else if (i == size/2 && j == size/2-1) { buttons[j][i].setBackground(Color.blue); } } } //add other labels and buttons window.add(player); player.setForeground(Color.red); window.add(turn); redscore.setForeground(Color.red); window.add(redscore); window.add(redscoreval); bluescore.setForeground(Color.blue); window.add(bluescore); window.add(bluescoreval); } /** * This method performs an event when a button is clicked on and keeps track of the score. */ public void actionPerformed(ActionEvent event) { blue_value = 0; red_value = 0; //System.out.println("" + turnvalue); for (i = 0; i < size; i += 1) { for (j = 0; j < size; j += 1) { //check the source of the event if (event.getSource() == buttons[j][i]) { //if the button clicked is adjacent to a colored button if (clicks(i,j)) { if (buttons[j][i].getBackground() == Color.red || buttons[j][i].getBackground() == Color.blue) { //display the message turn.setText("Turn Still!"); } else if (buttons[j][i].getBackground() == Color.white) { turnvalue += 1; turn.setText("Turn"); //change color and keep track of which player's turn it is if (turnvalue%2 != 0) { buttons[j][i].setBackground(Color.red); player.setText("BLUE'S "); player.setForeground(Color.blue); } else if (turnvalue%2 == 0) { buttons[j][i].setBackground(Color.blue); player.setText("RED'S "); player.setForeground(Color.red); } change(i,j); } } } //count the number of blue and red squares in the game if (buttons[j][i].getBackground() == Color.blue) { blue_value += 1; bluescoreval.setText("" + blue_value); } else if (buttons[j][i].getBackground() == Color.red) { red_value += 1; redscoreval.setText("" + red_value); } //check to see if the game is over if (blue_value + red_value >= 64) { player.setForeground(Color.black); player.setText("Game "); turn.setText("Over"); } } } } /** * This method checks to see if a click is valid (i.e. next to a colored square). * * @param i - row of the clicked square * @param j - column of the clicked square * @return - true or false. depending on the location of the click. */ public boolean clicks(int i, int j) { if (j+1 < size && buttons[j+1][i].getBackground() != Color.white || i+1 < size && buttons[j][i+1].getBackground() != Color.white || (i+1 < size && j+1 < size) && buttons[j+1][i+1].getBackground() != Color.white || j-1 > -1 && buttons[j-1][i].getBackground() != Color.white || i-1 > -1 && buttons[j][i-1].getBackground() != Color.white || (i-1 > -1 && j-1 > -1) && buttons[j-1][i-1].getBackground() != Color.white || (i+1 < size && j-1 > -1) && buttons[j-1][i+1].getBackground() != Color.white || (i-1 > -1 && j+1 < size) && buttons[j+1][i-1].getBackground() != Color.white) { return true; } else { turn.setText("Turn Still!"); return false; } } /** * This method changes the colors of squares that are sandwiched between two opposite-colored squares in all directions. * * @param r - the row of the tested square. * @param c - the column of the tested square. */ public void change(int r, int c) { int i = r; int j = c; int goingbackcolumn; int goingbackrow; //upper right diagonal- works good for (j = c+1, i = r-1; j < size && i >= 0; j++, i--) { if (buttons[j][i].getBackground() == Color.white) { break; } if (buttons[j][i].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[j][i].getBackground() == buttons[c][r].getBackground()) { for (goingbackcolumn = j, goingbackrow = i; goingbackcolumn > c && goingbackrow < r; goingbackcolumn--, goingbackrow++) { buttons[goingbackcolumn][goingbackrow].setBackground(buttons[c][r].getBackground()); } } } //upper left diagonal - works good for (j = c-1, i = r-1; j >= 0 && i >= 0; j--, i--) { if (buttons[j][i].getBackground() == Color.white) { break; } if (buttons[j][i].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[j][i].getBackground() == buttons[c][r].getBackground()) { for (goingbackcolumn = j, goingbackrow = i; goingbackcolumn < c && goingbackrow < r; goingbackcolumn++, goingbackrow++) { buttons[goingbackcolumn][goingbackrow].setBackground(buttons[c][r].getBackground()); } } } //lower right diagonal - works good for (j = c+1, i = r+1; j < size && i < size; j++, i++) { if (buttons[j][i].getBackground() == Color.white) { break; } if (buttons[j][i].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[j][i].getBackground() == buttons[c][r].getBackground()) { for (goingbackcolumn = j, goingbackrow = i; goingbackcolumn > c && goingbackrow > r; goingbackcolumn--, goingbackrow--) { buttons[goingbackcolumn][goingbackrow].setBackground(buttons[c][r].getBackground()); } } } //lower left diagonal - works good for (j = c-1, i = r+1; j >= 0 && i < size; j--, i++) { if (buttons[j][i].getBackground() == Color.white) { break; } if (buttons[j][i].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[j][i].getBackground() == buttons[c][r].getBackground()) { for (goingbackcolumn = j, goingbackrow = i; goingbackcolumn < c && goingbackrow > r; goingbackcolumn++, goingbackrow--) { buttons[goingbackcolumn][goingbackrow].setBackground(buttons[c][r].getBackground()); } } } //up - works good for (i = r-1; i >= 0; i--) { if (buttons[c][i].getBackground() == Color.white) { break; } if (buttons[c][i].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[c][i].getBackground() == buttons[c][r].getBackground()) { for (goingbackrow = i; goingbackrow < r; goingbackrow++) { buttons[c][goingbackrow].setBackground(buttons[c][r].getBackground()); } } } //down - works good for (i = r+1; i < size; i++) { if (buttons[c][i].getBackground() == Color.white) { break; } if (buttons[c][i].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[c][i].getBackground() == buttons[c][r].getBackground()) { for (goingbackrow = i; goingbackrow > r; goingbackrow--) { buttons[c][goingbackrow].setBackground(buttons[c][r].getBackground()); } } } //left - works good for (j = c-1; j >= 0; j--) { if (buttons[j][r].getBackground() == Color.white) { break; } if (buttons[j][r].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[j][r].getBackground() == buttons[c][r].getBackground()) { for (goingbackcolumn = j; goingbackcolumn < c; goingbackcolumn++) { buttons[goingbackcolumn][r].setBackground(buttons[c][r].getBackground()); } } } //right - works good for (j = c+1; j < size; j++) { if (buttons[j][r].getBackground() == Color.white) { break; } if (buttons[j][r].getBackground() == oppositeColor(buttons[c][r].getBackground())) { continue; } if (buttons[j][r].getBackground() == buttons[c][r].getBackground()) { for (goingbackcolumn = j; goingbackcolumn > c; goingbackcolumn--) { buttons[goingbackcolumn][r].setBackground(buttons[c][r].getBackground()); } } } } /** * This method returns the opposite color of the sandwiching squares. * * @param c - the opposite color * @return - either Color.red or Color.blue */ public static Color oppositeColor(Color c) { if (c == Color.red) { return (Color.blue); } else if (c == Color.blue) { return (Color.red); } return (Color.white); } }