Huarong Road (Java)

Source: Internet
Author: User

The Java teacher decorate this huarong way of question, about two more weeks, on the machine, a class of classmates made out, in the heart feel very ... So Qingming went home, studied the study. Because before the C + + elective, so the class of what has a little bit of understanding of Java is not familiar with, anyway, so with eclipse made, where do not good, I hope to correct.

Huarong Road:

The general idea says: There is a people class, and the Board class, the board will each person's name, and then move according to the rules of movement, when the devil moved to the following to win.

People class:

Package people;

Import Java.util.Scanner;

public class People {
String name;
int state;//State, moving the same piece of state while moving
People (String c,int s)//constructor
{
This.name=c;
State=s;

}
static void Judgename (String name)//Determine if the name is entered correctly
{
int a=1;
while (a==1)
{
Switch (name)
{
Case "Caocao": a=0;break;
Case "Zhang Fei": a=0;break;
Case "Zhao Yun": a=0;break;
Case "Jack Huang": a=0;break;
Case "Guan Yu": a=0;break;
Case "Ma Chao": a=0;break;
Case "Bing": a=0;break;
Case "Shi": a=0;break;
Case "Wei": a=0;break;
Case "Stroke": a=0;break;
Default
{
SYSTEM.OUT.PRINTLN ("Name input error, please re-enter:");
Scanner reader= New Scanner (system.in);
Name=reader.next ();
Judgename (name);
Reader.close ();
}
}
}
}
}

Chessboard class:

Package people;

public class Chessboard {

People[][] Board=new people[4][5]; Applied for a people type of chessboard
Chessboard ()
{
Board[1][4]=new people ("Cao", 0);
Board[2][4]=new people ("F", 0);
Board[0][4]=new people ("Zhao", 1);
Board[0][3]=new people ("cloud", 1);
Board[0][2]=new people ("Yellow", 2);
Board[0][1]=new people ("Zhong", 2);
Board[0][0]=new people ("Bing", 3);
Board[1][2]=new people ("Ma", 4);
Board[2][2]=new people ("Super", 4);
Board[1][1]=new people ("Wei", 5);
Board[2][1]=new people ("Shi", 6);
Board[3][4]=new people ("off", 7);
Board[3][3]=new people ("feather", 7);
Board[3][2]=new people ("Zhang", 8);
Board[3][1]=new people ("Fei", 8);
Board[3][0]=new people ("stroke", 9);
Board[1][3]=new people ("", 10);
Board[2][3]=new people ("", 11);
Board[1][0]=new people ("", 12);
Board[2][0]=new people ("", 13);

}
void print ()//output
{
for (int j=4;j>=0;j--)
{for (int i=0;i<4;i++)
{System.out.printf ("" +board[i][j].name);
}
System.out.println ();
}
System.out.println ();
}

Boolean win ()//Win condition
{
if (board[1][1].name== "Cao" &&board[2][1].name== "Operation")
{
return true;
}
else return false;

}
Boolean judgemove (int i,int j,string dir)//determine if the conditions for moving are met
{
Switch (dir) {
Case "left":
{if (i<0| | i>4| | j<0| | j>5| | board[i-1][j].name== "")
{
return false;
}
Else
return true;}


Case "Right": {if (i<0| | i>4| | j<0| | j>5| | board[i+1][j].name== "")
{
return false;
}
Else
return true;}
Case "on":
{if (i<0| | i>4| | j<0| | j>5| | board[i][j+1].name== "")
{
return false;
}
Else
return true;}
Case "under":
{if (i<0| | i>4| | j<0| | j>5| | board[i][j-1].name== "")
{
return false;
}
Else
return true;}
}

return false;


}
Boolean Move (String dir,int i,int J)//Determine if it can be moved
{
Switch (dir)
{
Case "left": if (Judgemove (I,j,dir))
{
return false;
}
Else
{
Board[i-1][j].name=board[i][j].name;
Board[i][j].name= "";
return true;
}
Case "Right": if (Judgemove (I,j,dir))
{
return false;
}
Else
{
Board[i+1][j].name=board[i][j].name;
Board[i][j].name= "";
return true;
}
Case "on": if (Judgemove (I,j,dir))
{
return false;

}
Else
{board[i][j+1].name=board[i][j].name;
Board[i][j].name= "";
return true;
}
Case "under": if (Judgemove (I,j,dir))
{
return false;

}
Else
{
Board[i][j-1].name=board[i][j].name;
Board[i][j].name= "";
return true;
}
Default:return false;
}
}
void FindName (String name1,string name2,string dir)//Find the piece you want to move in the board and the name is not a word
{

loop:for (int i=0;i<4;i++)
{
for (int j=0;j<5;j++)
{
if (Board[i][j].name.equals (name1))
{

for (int m=0;m<4;m++)
{
for (int k=0;k<5;k++)
{
if (Board[m][k].name.equals (name2))
{

if (board[i][j].state==board[i][j-1].state&&board[i][j].state!=board[i+1][j-1].state)
{if (move (dir,m,k) && Move (DIR,I,J))
;
else System.out.println ("cannot be moved");
}
if (board[i][j].name== "Cao" &&board[i+1][j].name== "Operation")
{
if (Move (dir,i,j-1) &&move (dir,i,j) &&move (dir,i+1,j-1) &&move (dir,i+1,j))
;
else System.out.println ("cannot be moved");
}
Else
{
if (Move (dir,i,j) && Move (dir,m,k))
;

}
Break loop;
}
}
}
}
}
}
}
void Findname1 (String name1,string dir)//When the name is only one word moving the case
{
loop1:for (int i=0;i<4;i++)
{
for (int j=0;j<5;j++)
{
if (Board[i][j].name.equals (name1))
{
Move (DIR,I,J);
Break Loop1;
}
}
}
}
}
Main class:

Package people;

Import Java.util.Scanner;


public class Mainpro {
public static void Main (string[] args)
{
Chessboard Board=new chessboard ();
Board.print ();
while (! Board.win ())
{
System.out.println ("Please enter the character you want to move");
Scanner reader = new Scanner (system.in);
String Name=reader.next ();
People. Judgename (name);
if (Name.length () ==2)
{String name1=name.substring (0,1);
String name2=name.substring;
System.out.println ("Please enter the direction you want to move");
String A=reader.next ();
Board.findname (Name1,name2,a);
}
Else
{
String name1=name.substring (0,1);
System.out.println ("Please enter the direction you want to move");
String A=reader.next ();
Board.findname1 (Name1,a);
}
Board.print ();
}
}
}

Operation Result:


                                &N Bsp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                       20150405

By: Warm to insist

Huarong Road (Java)

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.