Java入门之tic-tac-toe

Written by Bruth_Lee in Southwest University of Science and Technology. 

本游戏仅为初学者练习Java所用,不具有游戏的真实性,如果想看该正版游戏,请戳点击打开链接

生气

import java.util.Scanner;

public class Primer_Java{
	public static void main(String[] args) {
	 Scanner in = new Scanner(System.in);
	//int[] number = new int[10];
	//int a;
	int i,j;
	final int SIZE = 3;//the size of SIZE is three
	//tic-tac-toe
	int[][] board = new int[SIZE][SIZE];
	System.out.println("Gameplayer_1 input 1 and Gameplayer_2 input 0");
	for(i=0;i<board.length;i++)//Input the element,1 representative x,0 representative o;
	{
		for(j=0;j<board[i].length;j++)
		{
			board[i][j] = in.nextInt();
		}
	}
	boolean GotResult = false;
	//Jude row column Diagonal diagonal line
	int cnt = 0;
	int num1 = 0;
	int num2 = 0;
	//Judge row and column
	for(i=0;i<board.length;i++)
	{
		 num1 = 0;
		 num2 = 0;
		for(j=0;j<board[i].length;j++)//row
		{
			if(board[i][j]==1) {
				num1++;
			}
		}
		for(j=0;j<board[i].length;j++)//column
		{
			if(board[j][i]==1) {
				num2++;
			}
		}
		if(num1 == SIZE || num2 == SIZE)
		{
			System.out.println("Gameplayer_1 win!");
			GotResult = true;
			break;
		}
	    if(num2 == 0 || num1 == 0)
		{
	    	System.out.println("Gameplayer_2 win!");
	    	GotResult = true;
	    	break;
		}
	}
	
	if(GotResult == false){ 
		 num1 = 0;
		 num2 = 0;
		//Judge diagonal diagonal line
		for(i=0;i<board.length;i++)
		{
			if(board[i][i] == 1) {
				num1++;
			}
			if(board[i][2-i] == 1) {
				num2++;
			}
		}
		if(num1==3 || num2 ==3) {
			System.out.println("Gameplayer_1 win!");
//			GotResult = true;
		}
		else if(num2 == 0 || num1 == 0) {
			System.out.println("Gameplayer_2 win!");
//			GotResult = true;
		}
		else {
			System.out.println("Draw!");
		}
		
	}
  }
}


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖无为

感谢你们的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值