一个简单的java井字棋游戏

由于课堂上老师讲了一些相关算法,我也自学了一些swing组件的使用,于是心血来潮开始自己写一些游戏code。

关于井字棋,大家都不陌生,主要是通过将井盘分为9块区域,一方一次选择一块区域,接着优先选择出连续的3块区域者获胜。

其中我将9块区域分为1,2,3,4,5,6,7,8,9(正常的话将第一位设置为0更好),那么可以赢得游戏的选择方式有:

横:1,2,3/4,5,6/7,8,9

竖:1,4,7/2,5,8/3,6,9

斜:1,5,9/3,5,7

我的方法是:

1,建立一个字符串数组,将以上方法放入一个数组中,同时建立一个int类型数组;

2,建立相关的双方选择区域字符串数组,每选择一块区域,则在数组中添加这块区域的名称(即数字字符串);

3,通过indexOf的方法(不懂的可以查询该方法的用法),来遍历确定能够完成的选择方式,进而在该int类型数组中通过+1的方式来确认各种方法的通用程度;

4,通过遍历寻找该int类型数组是否有存在值为3的下标存在,则完成该方法,就可以确定输赢。

附上我自己写的代码。

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class wellCover {

	private JFrame frame;
	private boolean jia = true;
	JButton btnNewButton,btnNewButton_1,btnNewButton_2,button,button_1,button_2,button_3,button_4,button_5;
	JLabel label;
	private static String[] team;
	private static String j[],y[];
	private static int p,q;
	private JButton button_8;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		team = new String[8];
		//横
		team[0] = "1,2,3";
		team[1] = "4,5,6";
		team[2] = "7,8,9";
		//竖
		team[3] = "1,4,7";
		team[4] = "2,5,8";
		team[5] = "3,6,9";
		//斜
		team[6] = "1,5,9";
		team[7] = "3,5,7";
		j = new String[5];
		for(int i = 0;i < j.length;i++)
			j[i] = "jia";
		y = new String[5];
		for(int i = 0;i < y.length;i++)
			y[i] = "yi";
		p=0;q=0;
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					wellCover window = new wellCover();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public void sortS(String a[]){
		int a1[] = new int[a.length];
		for(int i = 0;i < a.length;i++)//赋值
			a1[i] = Integer.parseInt(a[i]);
		for(int i = 0;i < a1.length -1;i++)//排序
			for(int j = i+1;j < a1.length;j++){
				if(a1[i] >a1[j]){
					int temp = a1[i];
					a1[i] = a1[j];
					a1[j] = temp;
				}
			}
		for(int i = 0;i < a1.length;i++)//排序完后继续赋值
			a[i] = String.valueOf(a1[i]);
	}
	public boolean win(String t[],String a[],String jia){
		//先排序,再组合,接着遍历找出相关的
		//遍历出所有的情况,接着有相同情况的,设置一个数组,下标所锁定的值加1
		int tn[] = new int[t.length];
		int index = 0;
		for(int i = 0;i < a.length;i++){
			for(int j = 0;j < t.length;j++){
				if((index = t[j].indexOf(a[i])) != -1){//说明该点在该方法中存在
					tn[j] += 1;//该方法次数出现+1
				}
			}
		}
		for(int i = 0;i < tn.length;i++){
			if(tn[i] == 3){
				//System.out.println(jia+"赢了!");
				JOptionPane.showConfirmDialog(frame, jia+"赢了", "恭喜"+jia,  
	                    JOptionPane.OK_OPTION);  
				return true;
			}	
		}
		
		return false;
	}
	/**
	 * Create the application.
	 */
	public wellCover() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("\u4E95\u5B57\u68CB\u5BF9\u6218");
		frame.setBounds(100, 100, 548, 523);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(48, 44, 435, 374);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		 btnNewButton = new JButton("1");
		 btnNewButton.setBackground(Color.yellow);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					btnNewButton.setBackground(new Color(248,248,255));
					j[p++]="1";
					win(team,j,"甲");
				}				
				else{
					btnNewButton.setBackground(new Color(0,0,0));
					y[q++]="1";
					win(team,y,"乙");
				}
					
				btnNewButton.setEnabled(false);
			}
		});
		btnNewButton.setBounds(0, 0, 144, 123);
		panel.add(btnNewButton);
		
		 btnNewButton_1 = new JButton("2");
		 btnNewButton_1.setBackground(Color.yellow);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					btnNewButton_1.setBackground(new Color(248,248,255));
					j[p++]="2";
					win(team,j,"甲");
				}
				else{
					btnNewButton_1.setBackground(new Color(0,0,0));
					y[q++]="2`";
					win(team,y,"乙");
				}
				btnNewButton_1.setEnabled(false);
			}
		});
		btnNewButton_1.setBounds(143, 0, 138, 123);
		panel.add(btnNewButton_1);
		
		 btnNewButton_2 = new JButton("3");
		 btnNewButton_2.setBackground(Color.yellow);
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					btnNewButton_2.setBackground(new Color(248,248,255));
					j[p++]="3";
					win(team,j,"甲");
				}
				else{
					btnNewButton_2.setBackground(new Color(0,0,0));
					y[q++]="3";
					win(team,y,"乙");
				}
				btnNewButton_2.setEnabled(false);
			}
		});
		btnNewButton_2.setBounds(281, 0, 138, 123);
		panel.add(btnNewButton_2);
		
		 button = new JButton("4");
		 button.setBackground(Color.yellow);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					button.setBackground(new Color(248,248,255));
					j[p++]="4";
					win(team,j,"甲");
				}
				else{
					button.setBackground(new Color(0,0,0));
					y[q++]="4";
					win(team,y,"乙");
				}
				button.setEnabled(false);
			}
		});
		button.setBounds(0, 120, 144, 123);
		panel.add(button);
		
		 button_1 = new JButton("5");
		 button_1.setBackground(Color.yellow);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					button_1.setBackground(new Color(248,248,255));
					j[p++]="5";
					win(team,j,"甲");
				}	
				else{
					button_1.setBackground(new Color(0,0,0));
					y[q++]="5";
					win(team,y,"乙");
				}
				button_1.setEnabled(false);
			}
		});
		button_1.setBounds(143, 120, 138, 123);
		panel.add(button_1);
		
		 button_2 = new JButton("6");
		 button_2.setBackground(Color.yellow);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					button_2.setBackground(new Color(248,248,255));
					j[p++]="6";
					win(team,j,"甲");
				}	
				else{
					button_2.setBackground(new Color(0,0,0));
					y[q++]="6";
					win(team,y,"乙");
				}
				button_2.setEnabled(false);
			}
		});
		button_2.setBounds(281, 120, 138, 123);
		panel.add(button_2);
		
		 button_3 = new JButton("7");
		 button_3.setBackground(Color.yellow);
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					button_3.setBackground(new Color(248,248,255));
					j[p++]="7";
					win(team,j,"甲");
				}
				else{
					button_3.setBackground(new Color(0,0,0));
					y[q++]="7";
					win(team,y,"乙");
				}
				button_3.setEnabled(false);
			}
		});
		button_3.setBounds(0, 241, 144, 123);
		panel.add(button_3);
		
		 button_4 = new JButton("8");
		 button_4.setBackground(Color.yellow);
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					button_4.setBackground(new Color(248,248,255));
					j[p++]="8";
					win(team,j,"甲");
				}
				else{
					button_4.setBackground(new Color(0,0,0));
					y[q++]="8";
					win(team,y,"乙");
				}
				button_4.setEnabled(false);
			}
		});
		button_4.setBounds(143, 241, 138, 123);
		panel.add(button_4);
		
		 button_5 = new JButton("9");
		 button_5.setBackground(Color.yellow);
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					button_5.setBackground(new Color(248,248,255));
					j[p++]="9";
					win(team,j,"甲");
				}
				else{
					button_5.setBackground(new Color(0,0,0));
					y[q++]="9";
					win(team,y,"乙");
				}
				button_5.setEnabled(false);
			}
		});
		button_5.setBounds(281, 241, 138, 123);
		panel.add(button_5);
		
		JLabel lblNewLabel = new JLabel("\u5BF9\u6218\u53CC\u65B9\uFF1A\u7532\uFF0C\u4E59");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(82, 0, 324, 31);
		frame.getContentPane().add(lblNewLabel);
		
		JButton button_6 = new JButton("\u6094\u68CB");
		button_6.setBounds(29, 438, 113, 27);
		frame.getContentPane().add(button_6);
		
		JButton button_7 = new JButton("\u4EA4\u6362\u53CC\u65B9");
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(jia){
					jia = false;
					label.setText("当前轮到乙");
					return;
				}
				else{
					jia = true;
					label.setText("当前轮到甲");
					return;
				}
				
			}
		});
		button_7.setBounds(156, 438, 113, 27);
		frame.getContentPane().add(button_7);
		
		label = new JLabel("\u5F53\u524D\u8F6E\u5230\u7532");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(309, 431, 174, 29);
		frame.getContentPane().add(label);
		
		button_8 = new JButton("\u91CD\u65B0\u5F00\u59CB");//重新开始按钮
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnNewButton.setEnabled(true);
				btnNewButton_1.setEnabled(true);
				btnNewButton_2.setEnabled(true);
				button.setEnabled(true);
				button_1.setEnabled(true);
				button_2.setEnabled(true);
				button_3.setEnabled(true);
				button_4.setEnabled(true);
				button_5.setEnabled(true);
				btnNewButton.setBackground(Color.yellow);
				btnNewButton_1.setBackground(Color.yellow);
				btnNewButton_2.setBackground(Color.yellow);
				button.setBackground(Color.yellow);
				button_1.setBackground(Color.yellow);
				button_2.setBackground(Color.yellow);
				button_3.setBackground(Color.yellow);
				button_4.setBackground(Color.yellow);
				button_5.setBackground(Color.yellow);
				//恢复默认值
				j = new String[5];
				for(int i = 0;i < j.length;i++)
					j[i] = "jia";
				y = new String[5];
				for(int i = 0;i < y.length;i++)
					y[i] = "yi";
				p=0;q=0;
			}
		});
		button_8.setBounds(14, 13, 113, 27);
		frame.getContentPane().add(button_8);
	}
}
第一次写博客!如果有缺点请大家踊跃批评我!~
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个简单Java 井字棋游戏示例代码: ```java import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char[][] board = new char[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = ' '; } } char currentPlayer = 'X'; boolean hasWinner = false; int moves = 0; while (!hasWinner && moves < 9) { // Print current board System.out.println("Current board:"); for (int i = 0; i < 3; i++) { System.out.println(board[i][0] + " | " + board[i][1] + " | " + board[i][2]); if (i < 2) { System.out.println("---------"); } } // Get current player's move int row, col; do { System.out.println("Player " + currentPlayer + ", enter row (0-2):"); row = scanner.nextInt(); System.out.println("Player " + currentPlayer + ", enter column (0-2):"); col = scanner.nextInt(); } while (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' '); // Make move board[row][col] = currentPlayer; moves++; // Check for winner if ((board[0][0] == currentPlayer && board[0][1] == currentPlayer && board[0][2] == currentPlayer) || (board[1][0] == currentPlayer && board[1][1] == currentPlayer && board[1][2] == currentPlayer) || (board[2][0] == currentPlayer && board[2][1] == currentPlayer && board[2][2] == currentPlayer) || (board[0][0] == currentPlayer && board[1][0] == currentPlayer && board[2][0] == currentPlayer) || (board[0][1] == currentPlayer && board[1][1] == currentPlayer && board[2][1] == currentPlayer) || (board[0][2] == currentPlayer && board[1][2] == currentPlayer && board[2][2] == currentPlayer) || (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) || (board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer)) { hasWinner = true; System.out.println("Player " + currentPlayer + " wins!"); } // Switch players currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } if (!hasWinner) { System.out.println("It's a tie!"); } scanner.close(); } } ``` 运行此程序,您将得到一个典型的井字棋游戏界面。每个玩家轮流输入他们的行和列选择,直到有一个玩家获胜或棋盘已满。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值