Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import java.util.Arrays;
- public class LinkedList {
- Node head;
- LinkedList(Model model,Update update){
- head=new Node(model,update);
- }
- }
- class Node {
- static int search=2;
- static int t=0;
- static boolean turn;
- boolean playerOneVictory;
- boolean playerTwoVictory;
- int sumPlayerOne;
- int sumPlayerTwo;
- int[][]currentGame;
- static Model model;
- static Update update;
- Node columnOne;
- Node columnTwo;
- Node columnThree;
- Node columnFour;
- Node columnFive;
- Node columnSix;
- Node columnSeven;
- String coordinate="";
- Node (Model model, Update update){
- this.model=model;
- this.update=update;
- turn=model.isPlayerTurn();
- if(turn==false) {
- addMoves(model.getGridAllDiscs(), 0,"start");
- }
- }
- Node(int[][] previousGrid, String coordinate, boolean playerOneVictory, boolean playerTwoVictory,int column, String coords, boolean turn){
- this.playerOneVictory=playerOneVictory;
- this.playerTwoVictory=playerTwoVictory;
- this.coordinate=coordinate;
- currentGame=previousGrid;
- t=t+1;
- System.out.println(t);
- String [] xy=coords.split(",");
- System.out.println(xy[0]+","+xy[1]);
- if(turn==true){
- currentGame[Integer.parseInt(xy[0])][Integer.parseInt(xy[1])]=1;
- }else{
- currentGame[Integer.parseInt(xy[0])][Integer.parseInt(xy[1])]=2;
- }
- if(search>-1&&search<9) {
- this.turn=!this.turn;
- addMoves(currentGame, column,coords);
- }
- }
- public void addMoves(int[][]currentState,int column, String coord){
- System.out.println(search+"Search");
- coordinate=coord;
- int[][]tempGrid=currentState;
- if(coord.equals("start")) {
- coordinate = "3,5";
- tempGrid[3][5]=2;
- columnOne=new Node(tempGrid,coordinate,playerOneVictory,playerTwoVictory,0,"0,5",turn);
- turn=!turn;
- }else{
- String[]temp=coordinate.split(",");
- int newY=Integer.parseInt(temp[1]);
- if(newY>0){
- newY = newY - 1;
- coordinate=column+","+newY;
- System.out.println("HEY");
- System.out.println(Arrays.deepToString(tempGrid).replace("], ", "]\n"));
- columnOne=new Node(tempGrid,coordinate,playerOneVictory,playerTwoVictory,0,"0,"+columnHeight(tempGrid,0),turn);
- System.out.println("HELLO");
- }}
- search=search-1;}
- public int columnHeight(int[][]grid,int column) {
- int height = 5;
- while (grid[column][height] != 0) {
- if(height!=0) {
- height = height - 1;
- }
- }
- return height;
- }
- public void checkFour(int[][]currentState){
- connectFourHorizontal(currentState);
- connectFourVertical(currentState);
- connectFourDiagonal(currentState);
- }
- public void connectFourHorizontal(int[][]currentState) {
- int[][]currentGrid=currentState;
- //If neither player has won
- if (playerOneVictory == false && playerTwoVictory == false) {
- //Double nested for loops to check the grid for four in a row horizontally.
- for (int j = 0; j < model.getYLength(); j++) {
- //Count for the amount of discs in a row for either player.
- sumPlayerOne = 0;
- sumPlayerTwo = 0;
- for (int i = 0; i < model.getXLength(); i++) {
- //If (i,j) is equal to 1 meaning a disc of player one is there.
- if (currentGrid[i][j] == 1) {
- //sumPlayerOne is incremented by 1.
- sumPlayerOne = sumPlayerOne + 1;
- sumPlayerTwo = 0;
- }
- //If (i,j) is equal to 2 meaning a disc of player two is there.
- if (currentGrid[i][j] == 2) {
- //sumPlayerOne is incremented by 1.
- sumPlayerTwo = sumPlayerTwo + 1;
- sumPlayerOne = 0;
- }
- //if (i,j) is empty.
- if (currentGrid[i][j] == 0){
- //sumPlayerTwo and sumPlayerOne is set to equal 0.
- sumPlayerOne = 0;
- sumPlayerTwo = 0;
- }
- //If sumPlayerOne is equal to 4 meaning there are four discs in a row.
- if (sumPlayerOne == 4) {
- //Player one wins
- playerOneVictory = true;
- }
- //If sumPlayerTwo is equal to 4 meaning there are four discs in a row.
- if (sumPlayerTwo == 4) {
- //Player two wins
- playerTwoVictory = true;
- }
- }
- }
- }
- }
- //Method used to check if four discs are in a row vertically.
- public void connectFourVertical(int[][]currentState) {
- int[][]currentGrid=currentState;
- //If neither player has won
- if (playerOneVictory == false && playerTwoVictory == false) {
- //Double nested for loops to check the grid for four in a row vertically.
- for (int i = 0; i < model.getXLength(); i++) {
- //Count for the amount of discs in a row for either player.
- sumPlayerOne = 0;
- sumPlayerTwo = 0;
- for (int j = 0; j < model.getYLength(); j++) {
- //If (i,j) is equal to 1 meaning a disc of player one is there.
- if (currentGrid[i][j] == 1) {
- //sumPlayerOne is incremented by 1.
- sumPlayerOne = sumPlayerOne + 1;
- //Sum of playerTwo discs reset
- sumPlayerTwo = 0;
- }
- //if (i,j) is either empty or contains a disc belonging to player two.
- if (currentGrid[i][j] == 0){
- //sumPlayerOne and sumPlayerTwo is set to equal 0.
- sumPlayerOne = 0;
- sumPlayerTwo = 0;
- }
- //If (i,j) is equal to 2 meaning a disc of player two is there.
- if (currentGrid[i][j] == 2) {
- //sumPlayerOne is incremented by 1.
- sumPlayerTwo = sumPlayerTwo + 1;
- //Sum of playerOne discs reset
- sumPlayerOne = 0;
- }
- //If sumPlayerOne is equal to 4 meaning there are four discs in a row.
- if (sumPlayerOne == 4) {
- //Player one wins
- playerOneVictory = true;
- }
- //If sumPlayerTwo is equal to 4 meaning there are four discs in a row.
- if (sumPlayerTwo == 4) {
- //Player two wins
- playerTwoVictory = true;
- }
- }
- }
- }
- }
- //Method used to check if four discs are in a row diagonally
- public void connectFourDiagonal(int[][]currentState) {
- int[][]currentGrid=currentState;
- //If neither player has won
- if (playerOneVictory == false && playerTwoVictory == false) {
- //Double nested for loop to check for four in a row diagonally to the right.
- for (int i = 0; i < model.getXLength() - 3; i++) {
- for (int j = 3; j < model.getYLength(); j++) {
- //If four discs are in a row diagonally to the right belonging to player one.
- if (currentGrid[i][j] == 1 && currentGrid[i + 1][j - 1] == 1 && currentGrid[i + 2][j - 2] == 1 && currentGrid[i + 3][j - 3] == 1) {
- //Player one wins.
- playerOneVictory = true;
- }
- //If four discs are in a row diagonally to the right belonging to player two.
- if (currentGrid[i][j] == 2 && currentGrid[i + 1][j - 1] == 2 && currentGrid[i + 2][j - 2] == 2 && currentGrid[i + 3][j - 3] == 2) {
- //Player two wins.
- playerTwoVictory = true;
- }
- }
- }
- //Double nested for loop to check for four in a row diagonally to the left.
- for (int x = 0; x < model.getXLength() - 3; x++) {
- for (int y = 0; y < model.getYLength() - 3; y++) {
- //If four discs are in a row diagonally to the left belonging to player one.
- if (currentGrid[x][y] == 1 && currentGrid[x + 1][y + 1] == 1 && currentGrid[x + 2][y + 2] == 1 && currentGrid[x + 3][y + 3] == 1) {
- //Player one wins.
- playerOneVictory = true;
- }
- //If four discs are in a row diagonally to the left belonging to player two.
- if (currentGrid[x][y] == 2 && currentGrid[x + 1][y + 1] == 2 && currentGrid[x + 2][y + 2] == 2 && currentGrid[x + 3][y + 3] == 2) {
- //Player two wins.
- playerTwoVictory = true;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement