Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- /**
- * @author Kevin George
- * @version 2/10/16
- */
- public class Game
- {
- int [][] board;
- int posX,posY,deltaX,deltaY;
- int spot,x,y,fail;
- public Game()
- {
- board = new int[9][9];
- posX = posY = 1;
- spot = 2;
- fail = 0;
- }
- private void createBoard()
- {
- for(y=0;y<9;y++)
- for(x=0;x<9;x++)
- board[y][x] = 0;
- /*
- for(y=1;y<9;y++)
- {
- for(x=1;x<9;x++)
- System.out.print(board[y][x] +" ");
- System.out.println();
- }
- System.out.println();*/
- }
- public void gamePlay()
- {
- board[1][1] = 1;
- int till64 = 0;
- boolean conti = true;
- do
- {
- createBoard();
- while(spot < 64)
- {
- move();
- fail++;
- if(fail>500)
- break;
- }
- System.out.println(spot);
- if(spot == 64)
- conti = false;
- else
- spot=2;
- //board = new int[9][9];
- posX = posY = 1;
- spot = 2;
- fail = 0;
- till64++;
- }while(conti);
- System.out.println();
- System.out.println(till64+" tries");
- }
- private void move()
- {
- boolean j = true;
- deltaX = randomNum();
- while(j)
- {
- deltaY = randomNum();
- if(Math.abs(deltaY) == Math.abs(deltaX))
- j=true;
- else
- j=false;
- }
- if((posX+deltaX)<=8&&(posY+deltaY)<=8&&(posX+deltaX)>=1&&(posY+deltaY)>=1&&board[posY+deltaY][posX+deltaX]==0)
- {
- posY+=deltaY;
- posX+=deltaX;
- board[posY][posX] = spot;
- spot++;
- }
- }
- private int randomNum()
- {
- int n=0;
- Random rand = new Random();
- while(n==0)
- {
- n = rand.nextInt(5) - 2;
- }
- return n;
- }
- public void printBoard()
- {
- for(y=1;y<9;y++)
- {
- for(x=1;x<9;x++)
- System.out.printf("%4d",board[y][x]);
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement