Well word chess code

Source: Internet
Author: User
Tags addall new set

Well word chess code

Package A;
Import java.util.ArrayList;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Scanner;


Import Java.util.Set;
	public class TT2 {private Scanner scan;  Set Set_all; All positions set[] Set_xo;  Both sides have been drop position int igo; Index of moves right side set[] Set_win;  All win pieces Force arrangement situation int IAI; Let AI hold which side int last_step;
		
		Where's the last step? Public TT2 () {scan = new Scanner (system.in);
		Set_all = new HashSet ();
		
		for (int i=0; i<9; i++) Set_all.add (i);
		Set_xo = new Set[2];
		Set_xo[0] = new HashSet ();
		SET_XO[1] = new HashSet ();
		
		igo = 0;
		int[][] tmp = {{0,1,2},{3,4,5},{6,7,8}, {0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
		Set_win = new Set[tmp.length];
			for (int i=0; i<tmp.length; i++) {Set_win[i] = new HashSet ();
		for (int j=0; j<tmp[i].length; j + +) Set_win[i].add (tmp[i][j));  } IAI =-1;  AI does not participate in last_step =-1; There is no final step}//>0 will win <0 = 0 draw private int Tryaigo () {if (set_all.size () = = Set_xo[0].size () + SET_XO[1].S Ize ()) return 0;0 of the side and 1 of the piece capacity equal to the Set t = new HashSet (); T.addall (Set_all)//Initialize chessboard T.removeall (set_xo[0]);//Remove moves position T.removeall (set_xo[1));//Remove Moves of other party int rr =-1;
				
				Suppose that you must lose for (Object k:t) {set_xo[igo].add (k);//try Walking try{if (test_finish () ==igo) return 1;
				igo = (igo+1)%2;
					try{int r = Tryaigo ();
					if (r<0) return 1;  if (r==0) rr = 0;  finally{igo = (igo+1)%2 was found in draw-go;
			Backtracking}} finally{Set_xo[igo].remove (k);
	} return RR; 
		}//Machine moves private int getairesult () {Set t = new HashSet ();
		T.addall (Set_all);
		T.removeall (Set_xo[0]);
		
		T.removeall (set_xo[1]);  List win = new ArrayList ();  Record all WINS Bureau Shifa List tie = new ArrayList ();
					Record all draw Shifa for (Object k:t) {set_xo[igo].add (k);//try to walk try{if (Test_finish () ==igo) {Win.add (k);
				Continue
				} igo = (igo+1)%2;
					try{int r = Tryaigo ();
					if (r<0) Win.add (k);
				if (r==0) Tie.add (k);
			} finally{		igo = (igo+1)%2;
			Backtracking}} finally{Set_xo[igo].remove (k);
		
		} if (Win.size () >0) return (Integer) win.get ((int) (Math.random () * win.size ()));
		
		if (Tie.size () >0) return (Integer) tie.get (int) (Math.random () * tie.size ()));
		Although the machine intelligence is negative, but the chessboard still can go, can take a random step, expect the opponent error.
	return Randomgo ();

		private int Randomgo () {if (set_all.size () = = Set_xo[0].size () + set_xo[1].size ()) return-1;	
		List t = new ArrayList ();
		T.addall (Set_all);
		T.removeall (Set_xo[0]);
		T.removeall (set_xo[1]);
	Return (Integer) t.get (int) (Math.random () * t.size ()));
			private void Show () {for (int i=0; i<9; i++) {if (i%3==0) System.out.println ();
			char c = '. ';
			if (Set_xo[0].contains (i)) c = ' x ';
			if (Set_xo[1].contains (i)) c = ' O ';
			if (i = = Last_step) c-= 32;
		System.out.print (c + "");
	} System.out.println ();
		private int Getplayerinput () {char c = ' x ';
		if (igo==1) c = ' O '; for (;;)
				{try{System.out.print ("please" + C + "Fang moves:"); Int n = (Iai==igo)?
				Getairesult (): Integer.parseint (Scan.nextline ()); if (n<0) return n;
				The Defeat if (Set_all.contains (n) ==false) throw new Exception ("");
				if (Set_xo[0].contains (n) | | set_xo[1].contains (n)) throw new Exception ("");
			return n;
			catch (Exception e) {System.out.println ("Incorrect input position ..."); }}//0, 1: There are winners and losers, 2: All the end of the game, draw,-1: There is no victory or defeat, you can continue under private int test_finish () {for (int i=0; i<set_win.length; i+
		
		+) {if (Set_xo[0].containsall (Set_win[i])) return 0;//x Sheng if (Set_xo[1].containsall (set_win[i)) return 1;//O win} if (set_xo[0].size () + set_xo[1].size () = = Set_all.size ()) return 2; Draw return-1;
		Continue}//set which side of AI holds public void Setai (int n) {if (n>1) return;
	IAI = n; public void Start () {for (;;)
			{Show (); int r = Test_finish (); Detect if the chess game should end if (r==0) System.out.println ("X side wins."
			"); if (r==1) System.out.println ("O side wins.")
			"); if (r==2) System.out.println ("End of Draw.")
			");
			
			if (r>=0) break; INT PI = getplayerinput (); if (pi<0) {char c = igo==0?
				' x ': ' O '; System.out.println (c + "side give up." Game over.
				");
			Break
			} set_xo[igo].add (PI);
			Last_step = PI;
		igo = (igo+1)%2;
		}} private static void Welcome () {System.out.println ("----------------");
		System.out.println ("Well word chess game coded by Gyhang");
		System.out.println ("Nine Sudoku with 0~8 numbers");
		System.out.println ("0 1 2");
		System.out.println ("3 4 5");
		System.out.println ("6 7 8"); System.out.println ("x side and O side take turns moves, x side go first, connect a line of side wins."
		");
	System.out.println ("------------------------------------------------------");
		public static void Main (string[] args) {welcome ();
		TT2 a = new TT2 ();  A.setai (0);  The machine goes first//a.setai (1);  People go first//a.setai (-1);
	The machine does not participate in A.start (); }
}


----------------
Well word chess game  coded by Gyhang
Nine Sudoku with 0~8 figures:
0 1 2 3 4 5 6 7 8
x square with o side turns moves, x side first go, The one who connects to a straight line wins.
------------------------------------------------------

. . . 
. . . 
. . . 
Please x Square moves:
x.. 
. . . 
. . . 
Please o Party moves: 4

x ... 
O. 
. . . 
Please x Fang moves:
x ... 
o x ... 
Please o side moves: 1

x o ... 
o x ... 
Please x Fang moves:
x o. 
o x 
. X. 
Please o Party moves: 8 x o x o 
please x side moves:
x o x. o x. x 
o 
moves: 3

x o x 
o o x
  . X o 
please x square moves:
x o x 
o o x x x 
o 
draw ends.



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.