人机博弈小游戏(Java)

人机博弈小游戏


萌新一个,自己写了一个猜拳的小游戏。正在学习中。。。

实现功能

电脑随机出拳
玩家任意出拳
五局三胜制
可判断最终赢家
下面展示 代码

// A code block
var foo = 'bar';
// An highlighted block
   package day0423;


import java.util.Random;
import java.util.Scanner;



public class Test02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
      Test02 fg = new Test02();
      fg.showMenu();
	}
	//功能菜单
	public void showMenu(){
		System.out.println("***欢迎进行猜拳小游戏***");
		System.out.println("游戏开始");
		Scanner key = new Scanner(System.in);
		System.out.println("请选择你的游戏对手(1.秦 2.婷 3.婷婷):");
		int num = key.nextInt();
		System.out.println("请输入玩家的姓名:");
		String name = key.next();
		String enemyName = "";
		switch (num) {
		case 1:
			enemyName = "秦";
			break;
		case 2:	
			enemyName = "婷";
		    break;
		case 3:
			enemyName = "婷婷";
			break;
		}
		System.out.println(name+"VS"+enemyName+"对战");
		System.out.println("确定开始吗?(按y开始,按其他键结束)");
		String temp = key.next();
		if(temp.equalsIgnoreCase("y")){
			play();
		}else{
			System.out.println("退出游戏,系统结束");
		}
	
	}
    public void play(){
    	Scanner key = new Scanner(System.in);
    	int count1=0;
    	int count2=0;
    	for(int i=0;i<5;i++){
    		System.out.println("***第"+(i+1)+"把***");
    		System.out.println("请出(1.石头2.剪刀3.布)");
    		int temp1=key.nextInt();
    		String myChoose = change(temp1);
    		Random rd = new Random();
    		int temp2 = rd.nextInt(3)+1;
    		String dChoose = change(temp2);
    		System.out.println("你出拳:"+myChoose);
    		System.out.println("系统出拳:"+dChoose);
    		if(temp1==temp2 ){
      		     System.out.println("平!");
      		     }else if(temp1==1&&temp2==3||temp1==2&&temp2==1||temp1==3&&temp2==2){
      		     System.out.println("敌人赢了!");
      		     count2++;
      		     }else{
      		     System.out.println("玩家赢了!");
                 count1++;
      		}
    		
    	}
    	if(count1>count2){
    	    System.out.println("最终结果你赢了!");
    	    }
    	else
    	if(count1==count2){
    		System.out.println("最终结果平局");
    	}
    	else 
    	if(count1<count2){
    		System.out.println("最终结果你输了!");
    	}
    }
    	public String  change(int Choose){
    		String str = "";
    		switch (Choose) {
    		case 1:
    			str = "石头";
    			break;
    		case 2:	
    			str = "剪刀";
    		    break;
    		case 3:
    			str = "布";
    			break;
    			
    	}
    		return str;
    		
    		}
    }

下面是运行结果在这里插入图片描述在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,以下是一个简单的Java五子棋人机博弈的示例代码: ```java import java.util.Scanner; public class GomokuGame { private static final int SIZE = 15; private static final char EMPTY = '-'; private static final char PLAYER = 'X'; private static final char COMPUTER = 'O'; private char[][] board; public GomokuGame() { board = new char[SIZE][SIZE]; initializeBoard(); } private void initializeBoard() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { board[i][j] = EMPTY; } } } private void printBoard() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } private boolean isBoardFull() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == EMPTY) { return false; } } } return true; } private boolean isWinningMove(int row, int col, char player) { // 检查行 int count = 0; for (int i = Math.max(0, col - 4); i <= Math.min(SIZE - 1, col + 4); i++) { if (board[row][i] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } // 检查列 count = 0; for (int i = Math.max(0, row - 4); i <= Math.min(SIZE - 1, row + 4); i++) { if (board[i][col] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } // 检查主对角线 count = 0; int startRow = Math.max(0, row - 4); int startCol = Math.max(0, col - 4); while (startRow <= Math.min(SIZE - 1, row + 4) && startCol <= Math.min(SIZE - 1, col + 4)) { if (board[startRow][startCol] == player) { count++; if (count == 5) { return true; } } else { count = 0; } startRow++; startCol++; } // 检查副对角线 count = 0; startRow = Math.min(SIZE - 1, row + 4); startCol = Math.max(0, col - 4); while (startRow >= Math.max(0, row - 4) && startCol <= Math.min(SIZE - 1, col + 4)) { if (board[startRow][startCol] == player) { count++; if (count == 5) { return true; } } else { count = 0; } startRow--; startCol++; } return false; } private boolean isValidMove(int row, int col) { return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == EMPTY; } private void makeMove(int row, int col, char player) { board[row][col] = player; } private void playGame() { Scanner scanner = new Scanner(System.in); boolean isPlayerTurn = true; while (true) { System.out.println("当前棋盘:"); printBoard(); if (isPlayerTurn) { System.out.println("玩家,请输入您的下棋位置(行 列,以空格分隔):"); int row = scanner.nextInt(); int col = scanner.nextInt(); if (isValidMove(row, col)) { makeMove(row, col, PLAYER); if (isWinningMove(row, col, PLAYER)) { System.out.println("玩家获胜!"); break; } isPlayerTurn = false; } else { System.out.println("无效的位置,请重新输入。"); } } else { System.out.println("电脑正在思考下棋位置..."); // 在这里实现电脑的下棋逻辑 // ... int row = 0; // 电脑下棋的行 int col = 0; // 电脑下棋的列 if (isValidMove(row, col)) { makeMove(row, col, COMPUTER); if (isWinningMove(row, col, COMPUTER)) { System.out.println("电脑获胜!"); break; } isPlayerTurn = true; } } if (isBoardFull()) { System.out.println("平局!"); break; } } scanner.close(); } public static void main(String[] args) { GomokuGame game = new GomokuGame(); game.playGame(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值