java数组小游戏——数三退一

java数组小游戏——数三退一

Count3Quit.java

//数3退1,小游戏算法测试
/*
	若干个人围成一圈,从第一个人开始数,数到第三个人,第三个人退出,
	继续从1开始数,到3退出
*/
public class Count3Quit {
	public static void main(String[] args) {
		
		//500个人拉成一圈,用数组模拟
		boolean[] arr = new boolean[500];
		for(int i=0; i<arr.length; i++) {
			arr[i] = true;
		}
				
		int leftCount = arr.length;//还剩多少个人
		int countNum = 0;//计数器
		int index = 0;//第0个人
		
		while(leftCount > 1) {
			if(arr[index] == true) {
				countNum ++;
				if(countNum == 3) {
					countNum = 0;
					arr[index] = false;
					leftCount --;
				}
			}
			
			index ++;
			
			//如果index等于arr.length,index回0
			if(index == arr.length) {
				index = 0;
			}
		}
		
		//找到剩下为true的数
		for(int i=0; i<arr.length; i++) {
			if(arr[i] == true) {
				System.out.println(i);//i=435,下标为435,为第436个人
			}
		}
	}
}

 

F:\java>javac Count3Quit.java

F:\java>java Count3Quit
435

F:\java>

 

 Count3Quit2.java

/*
	面向对象的写法:
	数三退一,500个小孩围成一圈
*/
public class Count3Quit2 {
	public static void main(String[] args) {
		//面向对象的写法
		KidCircle kc = new KidCircle(500);
		int countNum = 0;
		Kid k = kc.first;
		while(kc.count > 1) {
			countNum ++;
			if(countNum == 3) {
				countNum = 0;
				kc.delete(k);
			}
			k = k.right;
		}
		
		System.out.println(kc.first.id);
	}
}

//小孩类
class Kid {
	int id;
	Kid left;//小孩的左边
	Kid right;//小孩的右边
}

//小孩围成的圈
class KidCircle {
	int count = 0;
	Kid first, last;//开始的小孩和结束的小孩
	
	KidCircle(int n) {
		for(int i=0; i<n; i++) {
			add();
		}
	}
	
	//添加一个小孩
	void add() {
		Kid k = new Kid();
		k.id = count;
		if(count <= 0) {
			first = k;
			last = k;
			k.left = k;
			k.right = k;
		} else {
			last.right = k;
			k.left = last;
			k.right = first;
			first.left = k;
			last = k;
		}
		count ++;
	}
	
	//删除一个小孩
	void delete(Kid k) {
		if(count <= 0) {
			return;
		} else if (count == 1) {
			first = last = null;
		} else {
			k.left.right = k.right;
			k.right.left = k.left;
			
			if(k == first) {
				first = k.right;
			} else if( k == last) {
				last = k.left;
			}
		}
		count --;
	}
}

 

F:\java>javac Count3Quit2.java

F:\java>java Count3Quit2
435

F:\java>

 

 

以下是一个简单的扫雷小游戏JAVA 代码,实现了基本的游戏功能: ``` import java.util.Random; import java.util.Scanner; public class Minesweeper { private static final int ROWS = 10; private static final int COLS = 10; private static final int BOMBS = 10; private static final char COVERED = '-'; private static final char UNCOVERED = ' '; private static final char BOMB = '*'; private char[][] board; private boolean[][] bombPositions; public Minesweeper() { board = new char[ROWS][COLS]; bombPositions = new boolean[ROWS][COLS]; initBoard(); initBombs(); } private void initBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { board[i][j] = COVERED; } } } private void initBombs() { Random random = new Random(); int count = 0; while (count < BOMBS) { int row = random.nextInt(ROWS); int col = random.nextInt(COLS); if (!bombPositions[row][col]) { bombPositions[row][col] = true; count++; } } } private int countAdjacentBombs(int row, int col) { int count = 0; for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && bombPositions[i][j]) { count++; } } } return count; } public boolean playMove(int row, int col) { if (bombPositions[row][col]) { board[row][col] = BOMB; return false; } int adjacentBombs = countAdjacentBombs(row, col); board[row][col] = (char) ('0' + adjacentBombs); if (adjacentBombs == 0) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] == COVERED) { playMove(i, j); } } } } return true; } public void playGame() { Scanner scanner = new Scanner(System.in); boolean gameOver = false; while (!gameOver) { printBoard(); System.out.print("Enter row and column (e.g. 1 2): "); int row = scanner.nextInt() - 1; int col = scanner.nextInt() - 1; if (row < 0 || row >= ROWS || col < 0 || col >= COLS) { System.out.println("Invalid row or column!"); continue; } gameOver = !playMove(row, col); if (checkWin()) { System.out.println("You win!"); gameOver = true; } } printBoard(); } private boolean checkWin() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (board[i][j] == COVERED && !bombPositions[i][j]) { return false; } } } return true; } private void printBoard() { System.out.print(" "); for (int j = 0; j < COLS; j++) { System.out.print((j + 1) + " "); } System.out.println(); for (int i = 0; i < ROWS; i++) { System.out.print((i + 1) + " "); for (int j = 0; j < COLS; j++) { System.out.print(board[i][j] == UNCOVERED && bombPositions[i][j] ? BOMB : board[i][j]); System.out.print(" "); } System.out.println(); } } public static void main(String[] args) { Minesweeper minesweeper = new Minesweeper(); minesweeper.playGame(); } } ``` 代码中定义了游戏的行数,列数和雷数,以及用来表示板块状态的字符常量。在 `Minesweeper` 类中,通过二维数组 `board` 和 `bombPositions` 来表示游戏面板和雷的位置。通过 `initBoard` 和 `initBombs` 方法初始化游戏面板和雷的位置。 在 `countAdjacentBombs` 方法中,根据当前位置的周围八个位置计算雷的个数。 在 `playMove` 方法中,根据当前位置是否有雷计算该位置的状态,并根据周围是否有雷递归更新周围的位置状态。 在 `playGame` 方法中,通过 `Scanner` 类读取玩家的输入,并根据输入更新游戏状态,直到游戏结束。 在 `checkWin` 方法中,检查游戏是否胜利。 在 `printBoard` 方法中,输出游戏面板。 在 `main` 方法中,创建 `Minesweeper` 对象并调用 `playGame` 方法开始游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值