Java is suspected of snakes and ladders game

Source: Internet
Author: User


/**
* @Author JIANGFQ
*
*/
Package com.test;


Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Random;


/**
* @author JIANGFQ
*
*/
public class Game {
private static int[][] me = new int[1][1];
private static list<ladder> ladders = new arraylist<ladder> (4);
private static list<snake> snakes = new arraylist<snake> (4);
private static int[][] Datas = new INT[5][5];

/**
* @Author JIANGFQ
*
*/
public static void Main (string[] args) {
Init ();
Startgame ();

}


private static void Init () {
Me[0][0] = 1;
Random random = new random ();

int count = 1;
for (int i = 0; i < 5; i++) {
for (int j = 0; J < 5; J + +) {
DATAS[I][J] = count++;
}
}

for (int i = 0; i < 4; i++) {
int[][] Ladderheader = new Int[1][1];
int[][] Laddertail = new Int[1][1];
int HX = Random.nextint (5);
while (HX = = 0) {
HX = Random.nextint (5);
}
int hy = Random.nextint (5);

int tx = Random.nextint (5);
while (TX >= HX) {
tx = Random.nextint (5);
}
int ty = Random.nextint (5);

Ladderheader[0][0] = Datas[hx][hy];
Laddertail[0][0] = Datas[tx][ty];

Ladder Ladder = new Ladder (ladderheader,laddertail);
Ladders.add (Ladder);
}

for (int i = 0; i < 4; i++) {
int[][] Snakeheader = new Int[1][1];
int[][] Snaketail = new Int[1][1];
int HX = Random.nextint (5);
while (HX = = 0) {
HX = Random.nextint (5);
}
int hy = Random.nextint (5);

int tx = Random.nextint (5);
while (TX >= HX) {
tx = Random.nextint (5);
}
int ty = Random.nextint (5);
Snakeheader[0][0] = Datas[hx][hy];
Snaketail[0][0] = Datas[tx][ty];
Snake Snake = new Snake (snakeheader,snaketail);
Snakes.add (snake);
}

}

private static void Startgame () {
Random random = new random ();
Boolean flag = false;

while (!flag) {
int x = Random.nextint (7);
while (x = = 0) {
x = Random.nextint (7);
}
System.out.println ("You can jump:" + x + "Step!) Now in "+ me[0][0" + "going to go" + (x + me[0][0]);
int t = x + me[0][0];
for (int i = 0; i < datas.length; i++) {
Boolean isrun = false;
for (int j = 0; J < Datas[i].length; J + +) {
if (T >= 25) {
Flag = true;
Break
}
if (datas[i][j] = = t) {
for (Snake snake:snakes) {
int[][] headers = Snake.getheader ();
if (headers[0][0] = = t) {
Int[][] tails = Snake.gettail ();
System.out.println ("Go" + x + "step too back, encounter the head of the snake:" + t + "I want to slip to the tail of the Snake:" +tails[0][0]);
Me[0][0] = tails[0][0];
Isrun = true;
Break
}
}
for (Ladder ladder:ladders) {
Int[][] tails = Ladder.gettail ();
if (tails[0][0] = = t) {
int[][] headers = Ladder.getheader ();
System.out.println ("Go" +x + "step too good, met the ladder:" + t + "I want to climb up:" +headers[0][0]);
Me[0][0] = headers[0][0];
Isrun = true;
Break
}
}
if (!isrun) {
Me[0][0] = t;
Break
}
}
}
if (flag | | isrun) {
Break
}
}
}
System.out.println ("I have succeeded!" ");
}
}


Class Ladder {
Private int[][] header;
Private int[][] tail;
Public Ladder (int[][] header,int[][] tail) {
This.header = header;
This.tail = tail;
}
Public int[][] GetHeader () {
return header;
}
public void SetHeader (int[][] header) {
This.header = header;
}
Public int[][] GetTail () {
return tail;
}
public void Settail (int[][] tail) {
This.tail = tail;
}



}


Class Snake {
Private int[][] header;
Private int[][] tail;
Public Snake (int[][] header,int[][] tail) {
This.header = header;
This.tail = tail;
}
Public int[][] GetHeader () {
return header;
}
public void SetHeader (int[][] header) {
This.header = header;
}
Public int[][] GetTail () {
return tail;
}
public void Settail (int[][] tail) {
This.tail = tail;
}
}



Java is suspected of snakes and ladders game

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.