Use Java to implement a wuziqi game on the console

Source: Internet
Author: User

A five-game that I wrote a long time ago has been changed over the past two days. The Code is as follows:

 

Wuziqi. Java

Game. Java

Point. Java

Inpututil. Java

Mainclass. Java

 

Public class wuziqi {<br/> Private Static final int ROW = 9; <br/> Private Static final int column = 9; <br/> private int [] [] bord; <br/> private Boolean turn; <br/> Public wuziqi () {<br/> Init (); <br/>}< br/> Public void Init () {<br/> bord = new int [row] [column]; <br/> turn = true; <br/>}< br/> Public void printbord () {<br/> system. out. println ("************************************* ***************"); <br/> caffe M. out. print (""); <br/> for (INT I = 0; I <9; I ++) {<br/> system. out. print (char) ('A' + I) + ""); <br/>}< br/> system. out. println (); <br/> for (INT I = 0; I <9; I ++) {<br/> system. out. print (I); <br/> for (Int J = 0; j <9; j ++) {<br/> If (Bord [I] [J] = 0) {<br/> system. out. print ("_"); <br/>} else if (Bord [I] [J] = 1) {<br/> system. out. print ("X"); <br/>} else if (Bord [I] [J] =-1) {<br/> system. out. print ("O"); <br/>}< br/> SY Stem. out. println (); <br/>}< br/> system. out. println ("************************************* ***************"); <br/>}</P> <p> Public point getinput () {<br/> int X; <br/> int y; <br/> while (true) {<br/> system. out. print ("input the position:"); <br/> scanned scan = new partition (system. in); <br/> string input = scan. next (); <br/> X = integer. valueof (input. substring (0, 1); <br/> Y = integer. valueof (input. charat (1) -'A'); <br/> system. out. println (x + "" + Y); <br/> If (validation (X, Y, 0) {<br/> break; <br/>} else {<br/> system. out. println ("your input is invalidation! Try again "); <br/>}< br/> return new point (x, y ); <br/>}< br/> Public void playchess () {</P> <p> Init (); <br/> while (true) {<br/> printbord (); <br/> point P = NULL; <br/> If (turn) {<br/> system. out. println (","); <br/> P = getinput (); <br/> bord [p. getx ()] [p. gety ()] = 1; <br/>} else {<br/> system. out. println (","); <br/> P = getinput (); <br/> bord [p. getx ()] [p. gety ()] =-1; <br/>}< br/> If (iswin (P. getx (), P. gety (), Bord [p. getx ()] [p. gety ()]) {<br/> printbord (); <br/> announcewinner (); <br/> Init (); <br/> break; <br/>}< br/> turn =! Turn; <br/>}< br/> Public void announcewinner () {<br/> If (turn) {<br/> system. out. println ("White wins... "); <br/>}else {<br/> system. out. println ("Black wins... "); <br/>}< br/> Public Boolean validation (int x, int y, int type) {<br/> If (x <0 | x> = row | Y <0 | Y> = column) {<br/> return false; <br/>}< br/> If (Bord [x] [Y] = type) {<br/> return true; <br/>}< br/> return false; <br/>}< br/> Public Boolean iswin (int x, int y, int type) {<br/> // search for eight directions <br/> int COUNT = 0; <br/> int K = 0; <br/> int L = 0; <br/> // calculate in the first direction <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> K --; <br/> L --; <br/>}< br/> // calculate in the 8 direction <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> K ++; <br/> L ++; <br/>}< br/> If (count> = 6 | count <=-6) {<br/> return true; <br/>}< br/> // calculate in the direction of 2 <br/> COUNT = 0; <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> K --; <br/>}< br/> // calculate in the 7 direction <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> K ++; <br/>}< br/> If (count> = 6 | count <=-6) {<br/> return true; <br/>}< br/> // calculate in the 3 direction <br/> COUNT = 0; <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> K --; <br/> L ++; <br/>}< br/> // calculate in 6 directions <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> K --; <br/> L ++; <br/>}< br/> If (count> = 6 | count <=-6) {<br/> return true; <br/>}< br/> // count to 4 <br/> COUNT = 0; <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> L ++; <br/>}< br/> // calculate in the 5 direction <br/> K = x; <br/> L = y; <br/> for (INT I = 0; I <5; I ++) {<br/> If (validation (K, L, type )) {<br/> count ++; <br/>}else {<br/> break; <br/>}< br/> L --; <br/>}< br/> If (count> = 6 | count <=-6) {<br/> return true; <br/>}< br/> return false; <br/>}< br/> 

 

Public class game {<br/> private wuziqi; </P> <p> Public game () {<br/> This. wuziqi = new wuziqi (); <br/>}< br/> Public void start () {</P> <p> int choice = 0; <br/> while (true) {<br/> instructions (); <br/> system. out. print ("select:"); <br/> choice = inpututil. getint (); <br/> If (choice = 0) {<br/> system. out. println ("exited"); <br/> system. exit (0); <br/>}else if (choice = 1) {<br/> wuziqi. playchess (); <br/ >}Else if (choice = 2) {<br/> help (); <br/>}else {<br/> system. Out. println ("error! "); <Br/>}< br/> Public void instructions () {<br/> system. out. println ("Welcome to Wuzi game: 0-exit, 1-start, 2-help"); <br/>}< br/> Public void help () {<br/> system. out. println ("help information! "); <Br/>}< br/> 

 

/* <Br/> * Indicates a point on the Board <br/> */<br/> public class point {<br/> private int X; <br/> private int y; </P> <p> Public point () {</P> <p >}< br/> Public point (int x, int y) {<br/> This. X = x; <br/> This. y = y; <br/>}< br/> Public int getx () {<br/> return X; <br/>}< br/> Public void setx (int x) {<br/> This. X = x; <br/>}< br/> Public int Gety () {<br/> return y; <br/>}< br/> Public void sety (INT y) {<br/> This. y = y; <br/>}</P> <p >}< br/> 

 

Import Java. util. required; <br/> public class inpututil {<br/> Private Static writable scan = new writable (system. in); </P> <p> Public static int getint () {<br/> return scan. nextint (); <br/>}</P> <p> Public static string getstring () {<br/> return scan. next (); <br/>}< br/>} 

 

Public class mainclass {<br/> Public static void main (string [] ARGs) {<br/> New Game (). start (); <br/>}< br/>} 

 

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.