Skip to content

Commit

Permalink
NT/YX issue ucsb-cs56-projects#14 created working single player mode …
Browse files Browse the repository at this point in the history
…with random computer move
  • Loading branch information
meredithxu committed Mar 4, 2018
1 parent 7a721de commit e8cb3ed
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 160 deletions.
28 changes: 14 additions & 14 deletions src/edu/ucsb/cs56/projects/games/gomoku/CheckWins.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,14 @@ public static int checkLowerDownwardsDiagonals(int[][] boardToCheck, boolean pla
// OR Standard Gomoku, which requires EXACTLY five in a
// row.
if (maxInARow == 5 && playStandard) {
if (x < 15 && y < 15){
if( x == 14 || y == 14){
return lastColor;
} else if( boardToCheck[x + 1][y + 1] != lastColor){
return lastColor;
if (x < 15 && y < 15) {
if (x == 14 || y == 14) {
return lastColor;
} else if (boardToCheck[x + 1][y + 1] != lastColor) {
return lastColor;
}
}
}


}

// Update lastcolor
Expand Down Expand Up @@ -189,14 +189,14 @@ public static int checkHorizontalWin(int[][] boardToCheck, boolean playStandard)
// OR Standard Gomoku, which requires EXACTLY five in a
// row.
if (maxInARow == 5 && playStandard) {
if (x < 15){
if(x == 14){
return lastColor;
} else if (boardToCheck[x + 1][y] != lastColor){
return lastColor;
if (x < 15) {
if (x == 14) {
return lastColor;
} else if (boardToCheck[x + 1][y] != lastColor) {
return lastColor;
}
}
}


}

// Update lastcolor
Expand Down
5 changes: 3 additions & 2 deletions src/edu/ucsb/cs56/projects/games/gomoku/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Controller {

// declare the variables
Gomoku panel = new Gomoku();
GomokuSinglePlayer singlePanel = new GomokuSinglePlayer();
GomokuSinglePlayer singlePanel = new GomokuSinglePlayer();
int xCoord, yCoord;

/**
Expand All @@ -35,7 +35,8 @@ public Controller(Gomoku panel) {
yCoord = 0;
this.panel = panel; // for later uses
}
public Controller(GomokuSinglePlayer singlePanel) {

public Controller(GomokuSinglePlayer singlePanel) {
// set the default to zero
xCoord = 0;
yCoord = 0;
Expand Down
100 changes: 50 additions & 50 deletions src/edu/ucsb/cs56/projects/games/gomoku/Gomoku.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,33 +247,33 @@ public void paintComponent(Graphics g) {
// in order to comply with the MouseListener interface
public void mouseClicked(MouseEvent mouse) {
int win = CheckWins.checkForWin(grid, playStandard);
if (win == 0){
Controller c = new Controller(this);
c.coordinate(mouse);
// if statement to check if within bounds
if (c.getXCoord() < boardSize.x && c.getYCoord() < boardSize.y) {
// checks if there is already a piece on the spot
if (grid[c.getXCoord()][c.getYCoord()] != 1 && grid[c.getXCoord()][c.getYCoord()] != 2) {
xc = c.getXCoord();
yc = c.getYCoord();
// if no piece then colors that piece
setGrid(c.getXCoord(), c.getYCoord(), getCurrentColor());
// Switch player
if (getCurrentColor() == 1) {
setCurrentColor(2);
if (win == 0) {
Controller c = new Controller(this);
c.coordinate(mouse);
// if statement to check if within bounds
if (c.getXCoord() < boardSize.x && c.getYCoord() < boardSize.y) {
// checks if there is already a piece on the spot
if (grid[c.getXCoord()][c.getYCoord()] != 1 && grid[c.getXCoord()][c.getYCoord()] != 2) {
xc = c.getXCoord();
yc = c.getYCoord();
// if no piece then colors that piece
setGrid(c.getXCoord(), c.getYCoord(), getCurrentColor());
// Switch player
if (getCurrentColor() == 1) {
setCurrentColor(2);
} else {
setCurrentColor(1);
}
repaint();
} else {
setCurrentColor(1);
// throws error message if they try to put their piece on
// another
// player's
JOptionPane.showMessageDialog(null, "You can't put your pieces over other players!", "Error",
JOptionPane.ERROR_MESSAGE);
}
repaint();
} else {
// throws error message if they try to put their piece on
// another
// player's
JOptionPane.showMessageDialog(null, "You can't put your pieces over other players!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}

/** empty for now */
Expand All @@ -282,13 +282,13 @@ public void mouseEntered(MouseEvent mouse) {

/** empty for now */
public void mouseExited(MouseEvent mouse) {
int win = CheckWins.checkForWin(grid, playStandard);
if (win == 0){
if (grid[preX][preY] != 1 && grid[preX][preY] != 2) {
setGrid(preX, preY, 0);
repaint();
int win = CheckWins.checkForWin(grid, playStandard);
if (win == 0) {
if (grid[preX][preY] != 1 && grid[preX][preY] != 2) {
setGrid(preX, preY, 0);
repaint();
}
}
}
}

/** empty for now */
Expand All @@ -305,32 +305,32 @@ public void mouseDragged(MouseEvent mouse) {

/** empty for now */
public void mouseMoved(MouseEvent mouse) {
int win = CheckWins.checkForWin(grid, playStandard);
if (win == 0){
Controller c = new Controller(this);
c.coordinate(mouse);
if (c.getXCoord() < boardSize.x && c.getYCoord() < boardSize.y) {
if (grid[c.getXCoord()][c.getYCoord()] != 1 && grid[c.getXCoord()][c.getYCoord()] != 2) {
if (grid[preX][preY] == 3 || grid[preX][preY] == 4) {
setGrid(preX, preY, 0);
int win = CheckWins.checkForWin(grid, playStandard);
if (win == 0) {
Controller c = new Controller(this);
c.coordinate(mouse);
if (c.getXCoord() < boardSize.x && c.getYCoord() < boardSize.y) {
if (grid[c.getXCoord()][c.getYCoord()] != 1 && grid[c.getXCoord()][c.getYCoord()] != 2) {
if (grid[preX][preY] == 3 || grid[preX][preY] == 4) {
setGrid(preX, preY, 0);
repaint();
}
if ((getCurrentColor() == 1) && (grid[c.getXCoord()][c.getYCoord()] != 1)) {
setGrid(c.getXCoord(), c.getYCoord(), 3);
} else if (getCurrentColor() == 2 && (grid[c.getXCoord()][c.getYCoord()] != 2)) {
setGrid(c.getXCoord(), c.getYCoord(), 4);
}
repaint();
preX = c.getXCoord();
preY = c.getYCoord();
}
if ((getCurrentColor() == 1) && (grid[c.getXCoord()][c.getYCoord()] != 1)) {
setGrid(c.getXCoord(), c.getYCoord(), 3);
} else if (getCurrentColor() == 2 && (grid[c.getXCoord()][c.getYCoord()] != 2)) {
setGrid(c.getXCoord(), c.getYCoord(), 4);
} else {
if (grid[preX][preY] != 1 && grid[preX][preY] != 2) {
setGrid(preX, preY, 0);
repaint();
}
repaint();
preX = c.getXCoord();
preY = c.getYCoord();
}
} else {
if (grid[preX][preY] != 1 && grid[preX][preY] != 2) {
setGrid(preX, preY, 0);
repaint();
}
}
}
}

/**
Expand Down
Loading

0 comments on commit e8cb3ed

Please sign in to comment.