码迷,mamicode.com
首页 > 编程语言 > 详细

java实现简陋五子棋(虽然bug很多,但会继续改进)

时间:2015-04-11 21:00:32      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:五子棋   import   小游戏   计算机   java   

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 * 五子棋小游戏.可供双人对战和人机对战(人机对战的机是随机输入,未加如入精确算法)和棋的情况没有做判断。
 * <p>
 * 在15*15的棋盘上,获胜的情况总共有572种。<br>
 * 在一场五子棋的游戏中,计算机必须要知道有哪些的获胜组合,因此我们必须求得获胜组合的总数。<br>
 * (1)计算水平方向的获胜组合数,每一行的获胜组合是:11种,共15行,所以水平方向的获胜组合数为:11*15=165. <br>
 * (2)计算垂直方向的获胜组合总数,每一列的获胜组合是:11种,共15列,所以垂直方向的获胜组合数为:11*15=165.<br>
 * (3)计算正对角线方向的获胜组合总数,正对角线上的获胜组合总数为11+(10+9+...+2+1)*2=121.<br>
 * (4)计算反对角线方向的获胜组合总数,反对角线上的获胜组合总数为11+(10+9+...+2+1)*2=121.<br>
 * 所以,获胜的总数有165+165+121+121=572.
 * 
 * @author frank
 *
 */
public class NewGoBang
{
	// 静态代码块
	static
	{
		System.out.println("欢迎来到五子棋世界!");
	}
	public final static int size = 15;
	String[][] board = new String[size][size];
	boolean flag = true;
	// 创建一个random随机对象。
	Random ran = new Random();

	/**
	 * 画出棋盘。"十"
	 */
	public void initBoard()
	{
		for (int x = 0; x < board.length; x++)
		{
			for (int y = 0; y < board.length; y++)
			{
				board[x][y] = "十";// ╂ ,╋
			}
		}
		printArray(board);
	}

	/**
	 * 判断棋子是否有五个连续.
	 * 
	 * @param row
	 *            棋子的横坐标
	 * @param column
	 *            棋子的纵坐标
	 * @param str
	 *            字符串数组
	 * @param c
	 *            字符串参数(即是棋子)
	 */
	public void isFive(int row, int column, String[][] str, String c)
	{
		// 横向判断
		int m, L;
		m = 1;
		L = 1;
		while ((column + m) < size && str[row][column + m] == c)
		{
			L++;
			m++;
		}
		m = 1;
		while ((column - m) >= 0 && str[row][column - m] == c)
		{
			L++;
			m++;
		}

		showMessage(L, c);

		// 纵向判断
		m = 1;
		L = 1;
		while ((row + m) < size && str[row + m][column] == c)
		{
			L++;
			m++;
		}
		m = 1;
		while ((row - m) >= 0 && str[row - m][column] == c)
		{
			L++;
			m++;
		}

		showMessage(L, c);


		// 斜向判断
		Italic(row, column, str, c);
	}

	// 斜向判断
	public void Italic(int row, int column, String[][] str, String c)
	{

		int L = 1;// 定义一个指针,超过五个就胜利了。
		int m = 1;
		// 斜向判断
		while ((row + m) < size && (column + m) < size
				&& str[row + m][column + m] == c)
		{
			L++;
			m++;
		}
		m = 1;//m重新置为1;
		while ((row - m) >= 0 && (column - m) >= 0
				&& str[row - m][column - m] == c)
		{
			L++;
			m++;
		}

		showMessage(L, c);


		// 反斜向判断
		m = 1;
		L = 1;
		while ((row + m) < size && (column - m) >= 0
				&& str[row + m][column - m] == c)
		{
			L++;
			m++;
		}
		m = 1;//m重新置为1;
		while ((row - m) >= 0 && (column + m) < size
				&& str[row - m][column + m] == c)
		{
			L++;
			m++;
		}

		showMessage(L, c);


	}

	//判断是否等于五个
	public void showMessage(int point, String c)
	{
		if (point >= 5)
		{
			System.out.println( "恭喜 " + c + " 棋赢了!");
			//可以用下面这句弹出一个对话框,比较美观。			
			//JOptionPane.showMessageDialog(null, "恭喜 " + c + " 棋赢了!");
			//赢了之后当然得结束程序。
			System.exit(0);
		} else
		{
			point = 0;
		}
	}
        /*这里可以选择双人对战或者是人机对战。
            别太兴奋,人机对战只是随机输入的,并没有加入什么核心算法。
            后续有机会我会继续更新的。
        */
	public void show()
	{
		System.out.println("双人对战请输入:1,人机对战请输入2.");
		//利用Scanner输入数据。
		Scanner sc = new Scanner(System.in);
		int option = sc.nextInt();
		//这里可以选择双人对战或者是人机对战。
		switch(option)
		{
		            //双人对战
			case 1:
				while (flag)
				{
					System.out.println("请黑方输入棋子的坐标:(如:1 3)");
					manInput("●");
					System.out.println("请白方输入棋子的坐标:(如:1 3)");
					manInput("○");
				}
				//人机对战
			case 2:
				while (flag)
				{
					System.out.println("请您输入棋子的坐标:(如:1 3)");
					manInput("●");
					System.out.println("电脑下子:");
					computer("○");
				}
			//机机对战,用来快速调试
			case 3: 
				while (flag)
				{
					System.out.println("电脑A下子:");
					computer("●");
					System.out.println("电脑B下子:");
					computer("○");
				}
				
		}
		
	}
	
	//电脑随机下子。
	public boolean computer(String c)
	{
		int row = ran.nextInt(size);
		int column = ran.nextInt(size);
                
		while(board[row][column] != "十")
		{
			row = ran.nextInt(size);
			column = ran.nextInt(size);
		}
		if(board[row][column] == "十")
		{
			System.out.println("(" + row + "," + column + ")");
			board[row][column] = c;
			printArray(board);
			isFive(row, column, board, c);
		}
		else 
			return false;
		return true;
		
		
	}
	//人工下子。输入坐标Coordinate
	private void manInput(String c)// int row,int column
	{
		try
		{
			 Scanner scan = new Scanner(System.in);
			 int row = scan.nextInt();
			 int column = scan.nextInt();

			if (board[row][column] == "十")//row>=0&&column>=0&&row<size&&column<size&&
			{
				System.out.println("(" + row + "," + column + ")");
				board[row][column] = c;
				// 自选字符‘★‘,‘█‘,‘▓‘,‘‘,‘‘●●●‘╋‘‘"●"‘○
				printArray(board);
				isFive(row, column, board, c);
			} else
			{			
			
				System.out.println("该位置已放有棋子,请放到其他位置!");
				manInput(c);
			}
		} catch (ArrayIndexOutOfBoundsException e)
		{		
			System.out.println("超出棋盘位置,请重新输入!");
			manInput(c);
		}
		catch(InputMismatchException e)
		{
                //JOptionPane.showMessageDialog(null, "输入的数不合法,请重新输入!");
			System.out.println("输入的数不合法,请重新输入!");
			manInput(c);
		}
                // System.out.println("我是美丽的调试,我无处不在!");

	}

	// 打印String类型的二维数组
	private void printArray(String[][] arr)
	{
		for (int i = 0; i < arr.length; i++)
		{
			for (int j = 0; j < arr[i].length; j++)
			{
				System.out.print(arr[i][j]);
			}
			System.out.println("");
		}
	}

	public static void main(String[] args)
	{
		NewGoBang gobang = new NewGoBang();
		gobang.initBoard();
		gobang.show();
	}

}


本文出自 “卡暗哩” 博客,请务必保留此出处http://kaanli.blog.51cto.com/6744784/1631302

java实现简陋五子棋(虽然bug很多,但会继续改进)

标签:五子棋   import   小游戏   计算机   java   

原文地址:http://kaanli.blog.51cto.com/6744784/1631302

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!