菜鸟代码之TicTacToe二维数组解法

传说是BILL GATES的第一个程序,我就献丑了。人人对战相比较于人机对战真的简单好多,做这种小游戏真是要考虑好多好多,程序理解起来很简单,所以就不需要过多注释了。希望大家多多交流。

import java.util.*;

public class practice7_9 
{
    public static void main(String[] args)
    {
    	Scanner input = new Scanner(System.in);
    	
    	System.out.println("This is the funniest game ever.Are you ready?");
    	System.out.println("-------------");
    	System.out.println("|   |   |   |");
    	System.out.println("-------------");
    	System.out.println("|   |   |   |");
    	System.out.println("-------------");
    	System.out.println("|   |   |   |");
    	System.out.println("-------------");
    	
    	int[][]  game = new int[3][3];
    	int round = 1;
    	//gamer 1 use X
    	//gamer 2 use O
    	
    	System.out.println("Enter a row(1,2,or 3) for player 1 using X:");
        int row = input.nextInt();
        System.out.println("Enter a line(1,2,or 3) for player 1 using X");
        int line = input.nextInt();
        game[row - 1][line - 1] = 1;
    	
        while(!firstJudge(game))
    	{
    	    while(round % 2 == 1)
    	    {
    	    	if(round != 1)
    	    	{
    	    		  System.out.println("Enter a row(1,2,or 3) for player 1 using X:");
    	    	      row = input.nextInt();
    	    	      System.out.println("Enter a line(1,2,or 3) for player 1 using X");
    	    	      line = input.nextInt();
    	    	      game[row - 1][line - 1] = 1;
    	    	}
    	        for(int n = 0;n < 3;n++)
    	        {
    	        	System.out.println("-------------");
    	        	for(int m = 0;m < 3;m++)
    	        	{
    	        	    if(game[n][m] == 1)
    	        	    	System.out.format("| X ");
    	        	    else if(game[n][m] == -1)
    	        	    	System.out.format("| O ");
    	        	    else
    	        	    	System.out.format("|   ");
    	        	}
    	        	System.out.format("|");
    	        	System.out.format("\r");
    	        }
    	        System.out.println("-------------");
    	        round++;
    	        if(secondJudge(game) == 1)
    	    	{
    	    		System.out.println("Player1 wins!");
    	    		System.exit(0);
    	    	}
    	    }
    	    
    	    while(round % 2 == 0)
    	    {
    	    	System.out.println("Enter a row(1,2,or 3) for player 2 using O:");
    	        row = input.nextInt();
    	        System.out.println("Enter a line(1,2,or 3) for player 2 using O");
    	        line = input.nextInt();
    	        game[row - 1][line - 1] = -1;
    	    
    	        for(int n = 0;n < 3;n++)
    	        {
    	        	System.out.println("-------------");
    	        	for(int m = 0;m < 3;m++)
    	        	{
    	        	    if(game[n][m] == 1)
    	        	    	System.out.format("| X ");
    	        	    else if(game[n][m] == -1)
    	        	    	System.out.format("| O ");
    	        	    else
    	        	    	System.out.format("|   ");
    	        	}
    	        	System.out.format("|");
    	        	System.out.format("\r");
    	        }
    	        System.out.println("-------------");
    	        round++;
    	        if(secondJudge(game) == 2)
    	    	{
    	    		System.out.println("Player2 wins!");
    	    		System.exit(0);
    	    	}
    	    }
    	}
        if(secondJudge(game) == 1)
    	{
    		System.out.println("Player1 wins!");
    	}
    	else if(secondJudge(game) == 2)
    	{
    		System.out.println("Player2 wins!");
    	}
    }
    
    public static boolean firstJudge(int[][] array)
    {
    	int sumOfRow = 0,sumOfLine = 0;
    	int judge = 0;
    	for(int row = 0;row < 3;row++)
    	{
    		for(int line = 0;line < 3;line++)
    		{
    			sumOfRow += array[row][line];
    		}
    		if((sumOfRow == 3) || (sumOfRow == -3))
    		{
    		    judge = 1;
    			break;
    		}
    	}
    	
    	for(int line = 0;line < 3;line++)
    	{
    		for(int row = 0;row < 3;row++)
    		{
    			sumOfLine += array[row][line];
    		}
    		if((sumOfLine == 3) || (sumOfLine == -3))
    		{
    			judge = 1;
    		    break;
    		}
    	}
    	
    	for(int i = 0;i < 3;i++)
    	{
    		if((array[0][0]+array[1][1]+array[2][2] == 3) || (array[0][0]+array[1][1]+array[2][2] == -3) || (array[2][0]+array[1][1]+array[0][2] == 3) || (array[2][0]+array[1][1]+array[0][2] == -3))
    		{	
    			judge = 1;
    			break;
    		}
    	}
    	if(judge == 1)
    		return true;
    	else 
    		return false;
    }
    
    public static int secondJudge(int[][] array)
    {
    	int sumOfRow = 0,sumOfLine = 0,sumOfLeadingDiagonal = 0,sumOfDeputyDiagonal = 0;
    	int winner = 0;
    	
    	for(int row = 0;row < 3;row++)
    	{
    		for(int line = 0;line < 3;line++)
    		{
    			sumOfRow += array[row][line];
    		}
    		if((sumOfRow == 3) || (sumOfRow == -3))
    		{
    			break;
    		}
    	}
    	
    	for(int line = 0;line < 3;line++)
    	{
    		for(int row = 0;row < 3;row++)
    		{
    			sumOfLine += array[row][line];
    		}
    		if((sumOfLine == 3) || (sumOfLine == -3))
    		{
    		    break;
    		}
    	}
    	
    	for(int row = 0;row < 3;row++)
    	{
    		for(int line = 0;line < 3;line++)
    		{
    			if(row == line)
    			{
    			    sumOfLeadingDiagonal += array[row][line];
    			}
    			else if(row == 2 - line)
    			{
    				sumOfDeputyDiagonal += array[row][line];
    			}
    		}
    	}
    	
    	if((sumOfRow == 3) || (sumOfLine == 3) || (sumOfLeadingDiagonal == 3) || (sumOfDeputyDiagonal == 3))
    	{
    		winner = 1;
    	}
    	if((sumOfRow == -3) || (sumOfLine == -3) || (sumOfLeadingDiagonal == -3) || (sumOfDeputyDiagonal == -3))
    	{
    		winner = 2;
    	}
    	
    	return winner;
    }
    
    
}



 

你可能感兴趣的:(Java)