how to make it another player's turn java
Java
private boolean winner(int row, int col) {
// This is called just after a piece has been played on the
// square in the specified row and column. It determines
// whether that was a winning move by counting the number
// of squares in a line in each of the four possible
// directions from (row,col). If there are 5 squares (or more)
// in a row in any direction, then the game is won.
if (count( board[row][col], row, col, 1, 0 ) >= 5)
return true;
if (count( board[row][col], row, col, 0, 1 ) >= 5)
return true;
if (count( board[row][col], row, col, 1, -1 ) >= 5)
return true;
if (count( board[row][col], row, col, 1, 1 ) >= 5)
return true;
/* When we get to this point, we know that the game is not won. */
return false;
} // end winner()
void doClickSquare(int row, int col) {
// This is called by mousePressed() when a player clicks
// on the square in the specified row and col. It has already
// been checked that a game is, in fact, in progress.
/* Check that the user clicked an empty square. If not, show an
error message and exit. */
if ( board[row][col] != EMPTY ) {
if (currentPlayer == BLACK)
message.setText("BLACK: Please click an empty square.");
else
message.setText("WHITE: Please click an empty square.");
return;
}
/* Make the move. Check if the board is full or if the move
is a winning move. If so, the game ends. If not, then it's
the other user's turn. */
board[row][col] = currentPlayer; // Make the move.
Graphics g = getGraphics();
drawPiece(g, currentPlayer, row, col); // Draw the new piece.
g.dispose();
if (winner(row,col)) { // First, check for a winner.
if (currentPlayer == WHITE)
gameOver("WHITE wins the game!");
else
gameOver("BLACK wins the game!");
return;
}
boolean emptySpace = false; // Check if the board is full.
for (int i = 0; i < 13; i++)
for (int j = 0; j < 13; j++)
if (board[i][j] == EMPTY)
emptySpace = true; // The board contains an empty space.
if (emptySpace == false) {
gameOver("The game ends in a draw.");
return;
}
/* Continue the game. It's the other player's turn. */
if (currentPlayer == BLACK) {
currentPlayer = WHITE;
message.setText("WHITE: Make your move.");
}
else {
currentPlayer = BLACK;
message.setText("BLACK: Make your move.");
}
} // end doClickSquare()
private void drawPiece(Graphics g, int piece, int row, int col) {
// Draw a piece in the square at (row,col). The color
// is specified by the "piece" parameter, which should be
// either BLACK or WHITE.
if (piece == WHITE)
g.setColor(Color.white);
else
g.setColor(Color.black);
g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);
}
int ct = 1; // Number of pieces in a row belonging to the player.
int r, c; // A row and column to be examined
r = row + dirX; // Look at square in specified direction.
c = col + dirY;
while ( r >= 0 && r < 13 && c >= 0 && c < 13
&& board[r][c] == player ) {
// Square is on the board, and it
// contains one of the players's pieces.
ct++;
r += dirX; // Go on to next square in this direction.
c += dirY;
}
r = row - dirX; // Now, look in the opposite direction.
c = col - dirY;
while ( r >= 0 && r < 13 && c >= 0 && c < 13
&& board[r][c] == player ) {
ct++;
r -= dirX; // Go on to next square in this direction.
c -= dirY;
}
dirX dirY Why?
---- ---- --------------------------------
horizontal direction 1 0 Only x changes.
vertical direction 0 1 Only y changes.
first diagonal 1 1 Both x and y change.
second diagonal 1 -1 Change in opposing directions.
public void paint(Graphics g) {
/* Draw grid lines in darkGray. */
g.setColor(Color.darkGray);
for (int i = 1; i < 13; i++) {
g.drawLine(1 + 13*i, 0, 1 + 13*i, getSize().height);
g.drawLine(0, 1 + 13*i, getSize().width, 1 + 13*i);
}
/* Draw a two-pixel black border around the edges of the board. */
g.setColor(Color.black);
g.drawRect(0,0,getSize().width-1,getSize().height-1);
g.drawRect(1,1,getSize().width-3,getSize().height-3);
/* Draw the pieces that are on the board. */
for (int row = 0; row < 13; row++)
for (int col = 0; col < 13; col++)
if (board[row][col] != EMPTY)
drawPiece(g, board[row][col], row, col);
} // end paint()
g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);
Also in Java:
- Title
- deleting elements of an array in java
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- java how to read a text file
- Category
- Java
- Title
- java vs python
- Category
- Java
- Title
- random java
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- make a commet in java
- Category
- Java
- Title
- refrence xml textfield in javafx
- Category
- Java
- Title
- equls en java
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- java script print date in YYYY-MM-DD format
- Category
- Java
- Title
- Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /etc/java-8-openjdk
- Category
- Java
- Title
- JSONObject java
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- how to calculate min, max and average and write the output into into a text file in java
- Category
- Java
- Title
- spring framework iterate
- Category
- Java
- Title
- else statement java
- Category
- Java
- Title
- public static void main(string args)
- Category
- Java
- Title
- java string split underscore
- Category
- Java
- Title
- array in line java
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- how to find complement of a number in java
- Category
- Java
- Title
- get spring application context
- Category
- Java
- Title
- how to create a java jframe
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- java repository sql find not in list
- Category
- Java
- Title
- java singleton implementation
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- Filebody in java
- Category
- Java
- Title
- run spring boot application command line
- Category
- Java
- Title
- write in file java
- Category
- Java
- Title
- how to create a hashmap in java
- Category
- Java
- Title
- java shuffle list
- Category
- Java
- Title
- how to import jframe in java
- Category
- Java
- Title
- to char array java
- Category
- Java
- Title
- raspberry stackexchange how to install the java jdk
- Category
- Java
- Title
- java vs javascript
- Category
- Java
- Title
- demo java file
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- displaying an arraylist in java
- Category
- Java
- Title
- java how to iterate through a arraylist
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- print values of bst java
- Category
- Java
- Title
- all data types in java
- Category
- Java
- Title
- butterfly pattern program in java
- Category
- Java
- Title
- how to check if in array java
- Category
- Java
- Title
- close scanner java
- Category
- Java
- Title
- get material of block bukkit
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- java obtain list string from list object
- Category
- Java
- Title
- java get html from url
- Category
- Java
- Title
- How to loop through objects in java using streams
- Category
- Java
- Title
- how to push an element in hashset java
- Category
- Java
- Title
- Java system load from resources
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- treemap get order java
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- java new string with values
- Category
- Java
- Title
- number of matches regex java
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- uppercase java
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- arraylist java methds
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- hibernate Unknown integral data type for ids : java.lang.String
- Category
- Java
- Title
- array rotation program in java
- Category
- Java
- Title
- add video in bootstrap
- Category
- Java
- Title
- spigot sounds
- Category
- Java
- Title
- Java Read Files
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- difference between java and javax
- Category
- Java
- Title
- java 8 loop in map
- Category
- Java
- Title
- how to quicksort a string array in java
- Category
- Java
- Title
- how to break from a loop in java
- Category
- Java
- Title
- java string to double
- Category
- Java
- Title
- java find duplicates in array
- Category
- Java
- Title
- working with buttons in applet java
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- java check if file exists
- Category
- Java
- Title
- java convert a string to char[]
- Category
- Java
- Title
- fibonacci sequence java
- Category
- Java
- Title
- java loop through array
- Category
- Java
- Title
- get certain character from string java
- Category
- Java
- Title
- loop while in java
- Category
- Java
- Title
- how to create dynamic string array in java
- Category
- Java
- Title
- how to see if a shape is touching another shape in java
- Category
- Java
- Title
- String array into LinkedList java
- Category
- Java
- Title
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.security.MessageDigest$Delegate
- Category
- Java
- Title
- spigot get player from UUID
- Category
- Java
- Title
- java throws multiple exceptions
- Category
- Java
- Title
- java map foreach
- Category
- Java
- Title
- java hash password
- Category
- Java
- Title
- java arraylist add to top
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java