Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameState {
- //double check if static is correct. I think it is.
- private boolean turn; //true -> player1Turn false->player2Turn
- private int[][] currentBoard = new int[8][8];
- //Constructor
- GameState() {
- //init all to 0
- for (int a = 0; a < 8; a++) {
- for (int b = 0; b < 8; b++) {
- currentBoard[a][b] = 0;
- }
- }
- //Initialize the states of the beginning pieces (pieces in the middle)
- for (int i = 3; i < 5; i++) {
- for (int a = 3; a < 5; a++) {
- if (i == a) {
- currentBoard[i][a] = 1;
- } else {
- currentBoard[i][a] = 2;
- }
- }
- }
- }
- public void setTurn(boolean turnSet) {
- turn = turnSet;
- }
- public boolean getTurn() {
- return turn;
- }
- public int[][] getCurrentBoard() {
- return currentBoard;
- }
- public void updateCellArray(int row, int col, int state) {
- if (state == 0) {
- currentBoard[row][col] = 0;
- } else if (state == 1) {
- currentBoard[row][col] = 1;
- } else if (state == 2) {
- currentBoard[row][col] = 2;
- }
- }
- public int playerTurn(boolean turn) {
- if (turn) {
- return 1; //return white int
- } else {
- return 2;
- }
- }
- // Should create an array for this that has a true and false for each player. This should be only a couple lines long
- private boolean downDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
- return false;
- }
- //down
- if (x + 1 > 7) {
- return false;
- }
- if (currentBoard[x + 1][y] == opponentPlayer) { // if the piece below it is the opposing player's
- int num = 0;
- for (int a = 2; x + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x + a][y] == playerTurn) {
- num = (x + a);
- a = 8; // breaks outta loop bc of this
- }
- }
- int diff = num -x; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- System.out.println((x + b) + ", " + (y) + " Set to: " + playerTurn + " - downDirection");
- updateCellArray(x + b, y, playerTurn);// Sets everything in between equal
- }
- System.out.println("After updateCellArray in downDirection(2,2): " + currentBoard[2][2] );
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- private boolean upDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
- return false;
- }
- //checks up
- if (x - 1 < 0) {
- return false;
- }
- if (currentBoard[x - 1][y] == opponentPlayer) { // if the piece below it is the opposing player's
- int num = 0;
- for (int a = 2; x - a > 0; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x - a][y] == playerTurn) {
- num = (y + a);
- a = 8; // breaks outta loop bc of this
- }
- }
- int diff = num - y; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- System.out.println((x - b) + ", " + (y) + " Set to: " + playerTurn + " - upDirection");
- updateCellArray(x - b, y, playerTurn);// Sets everything in between equal
- }
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- private boolean leftDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) {
- return false;
- }
- //left
- if (y + 1 > 7) {
- return false;
- }
- if (currentBoard[x][y + 1] == opponentPlayer) { // if the piece right of it it is the opposing player's
- int num = 0;
- for (int a = 2; x + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x][y + a] == playerTurn) {
- num = (y + a);
- a = 8; // breaks outta loop bc of this
- }
- }
- int diff = num - y; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- System.out.println(x + ", " + (y + b) + " Set to: " + playerTurn + " - leftDirection");
- updateCellArray(x, y + b, playerTurn);// Sets everything in between equal
- }
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- private boolean rightDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
- return false;
- }
- if (y - 1 < 0) {
- return false;
- }
- //right
- if (currentBoard[x][y - 1] == opponentPlayer) { // if the piece below it is the opposing player's
- System.out.println("Recognizes that the piece left of it is the Opposing Player (in rightDirection Method)");
- int num = 0;
- for (int a = 2; x + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x][y - a] == playerTurn) {
- num = (y + a );
- System.out.println("currentBoard: " + x + " "+ (y-a) + "State:" + currentBoard[x][y-a]);
- a = 8; // breaks outta loop bc of this
- }
- }
- int diff = num - y; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- System.out.println(x + ", " + (y - b) + " Set to: " + playerTurn + " - rightDirection");
- updateCellArray(x, y - b, playerTurn);// Sets everything in between equal
- }
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- private boolean downLeftDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
- return false;
- }
- //down
- if (x + 1 > 7 || y + 1 > 7 ) {
- return false;
- }
- if (currentBoard[x + 1][y+1] == opponentPlayer) { // if the piece below it is the opposing player's
- int num = 0;
- for (int a = 2; x + a < 8 && y + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x + a][y + a] == playerTurn) {
- num = (x + a);
- a = 8; // breaks outta loop bc of this
- }
- }
- int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- updateCellArray(x + b, y + b, playerTurn);// Sets everything in between equal
- }
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- private boolean downRightDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
- return false;
- }
- //down
- if (x + 1 > 7 || y - 1 < 0 ) {
- return false;
- }
- if (currentBoard[x + 1][y-1] == opponentPlayer) { // if the piece below it is the opposing player's
- int num = 0;
- for (int a = 2; x + a < 8 && y - a > 0; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x + a][y - a] == playerTurn) {
- num = (x + a);
- a = 8; // breaks outta loop bc of this
- }
- }
- // THERE WILL PROBABLY BE AN ISSUE HERE WITH DIFF AND MIGHT NEED TO CREATE TWO OF THEM
- int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- updateCellArray(x + b, y - b, playerTurn);// Sets everything in between equal
- }
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- private boolean upRightDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
- return false;
- }
- //down
- if (x - 1 < 0 || y - 1 < 0 ) {
- return false;
- }
- if (currentBoard[x - 1][y-1] == opponentPlayer) { // if the piece below it is the opposing player's
- int num = 0;
- for (int a = 2; x - a > 0 && y - a > 0; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x - a][y - a] == playerTurn) {
- num = (x + a);
- a = 8; // breaks outta loop bc of this
- }
- }
- // THERE WILL PROBABLY BE AN ISSUE HERE WITH DIFF AND MIGHT NEED TO CREATE TWO OF THEM
- int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- updateCellArray(x - b, y - b, playerTurn);// Sets everything in between equal
- }
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- private boolean upLeftDirection(int rw, int cl) {
- int x = rw;
- int y = cl;
- int playerTurn = playerTurn(turn);
- int opponentPlayer = playerTurn(!turn);
- if (currentBoard[x][y] != 0) { // NEVER USE ANYTHING BESIDES CURRENTBOARD TO CHECK THE STATE OF THE BOARD!!!
- return false;
- }
- //down
- if (x - 1 < 0 || y + 1 > 7 ) {
- return false;
- }
- if (currentBoard[x - 1][y+1] == opponentPlayer) { // if the piece below it is the opposing player's
- int num = 0;
- for (int a = 2; x - a > 0 && y + a < 8; a++) {//look at the pieces below that (that's why a = 2, cause we already looked at 1)
- if (a == 7) {
- return false;
- }
- if (currentBoard[x - a][y + a] == playerTurn) {
- num = (x + a);
- a = 8; // breaks outta loop bc of this
- }
- }
- // THERE WILL PROBABLY BE AN ISSUE HERE WITH DIFF AND MIGHT NEED TO CREATE TWO OF THEM
- int diff = num - x; // Difference between the expected cell's y-value and the player's next vertical piece
- // ..next vertical piece below it...
- for (int b = 0; b < diff; b++) {
- updateCellArray(x - b, y + b, playerTurn);// Sets everything in between equal
- }
- if(num > 1){
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
- public boolean isPlaceable(int rw, int cl) {
- //have to implement if there is more than one as well that is true
- // System.out.println("Turn in isPlaceable: " + turn);
- if (downLeftDirection(rw, cl)) {
- // System.out.println("2,2: " +currentBoard[2][2]);
- System.out.println("downLeftDirectionTrue");
- return true;
- }else if (downRightDirection(rw, cl)) {
- System.out.println("downRightDirection true");
- return true;
- } else if (upRightDirection(rw, cl)) {
- System.out.println("downRightDirection true");
- return true;
- } else if (upLeftDirection(rw, cl)) {
- System.out.println("downRightDirection true");
- return true;
- }else if (downDirection(rw, cl)) {
- System.out.println("downDirection true");
- return true;
- }else if (upDirection(rw, cl)) {
- System.out.println("upDirection true");
- return true;
- } else if (leftDirection(rw, cl)) {
- System.out.println("leftDirection true");
- return true;
- } else if (rightDirection(rw, cl)) {
- System.out.println("rightDirection true");
- return true;
- } else {
- return false;
- }
- }
- public int PlayerTwoPoints() {
- int points = 0;
- for (int a = 0; a < 8; a++) {
- for (int b = 0; b < 8; b++) {
- if (currentBoard[a][b] == 2) {
- points++;
- }
- }
- }
- return points;
- }
- public int PlayerOnePoints() {
- int points = 0;
- for (int a = 0; a < 8; a++) {
- for (int b = 0; b < 8; b++) {
- if (currentBoard[a][b] == 1) {
- points++;
- }
- }
- }
- return points;
- }
- //look at a column, row, or diagonal. If there is a piece that is the player's color is blocked by an opponent's color and there is no empty piece in between, placeable is true
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement