一、前言
加强巩固JavaScript基础语法,现记录一下JavaScript的练习过程。
需要的模块:npm install readline-sync
配置:node.js
二、练习
▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰猜拳游戏(简易版)▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰
前置知识:函数、条件判断
注意:readline-sync
模块会有中文乱码问题,具体解决方案参考:https://codelove.tw/@hung/post/jaljmx
打开设置,选择区域
点击管理语言和设置:
勾选使用Unicode UTF-8提供全球语言支持,然后重启电脑。
代码如下:
const readline = require('readline-sync');
function computerRound() {
const random = Math.floor(Math.random() * 3);
return ['剪刀', '石头', '布'][random];
}
function playRound() {
while (true) {
console.log('请输入你的选择:剪刀、石头、布');
let userInput = readline.question('input:');
userInput = String(userInput).trim();
if (['剪刀', '石头', '布'].includes(userInput)) {
return userInput;
}
console.log('输入错误,请重新输入');
}
}
function checkGameOver(userScore, computerScore) {
if (userScore >= 3 || computerScore >= 3) {
const winner = userScore > computerScore ? '你' : '电脑';
console.log(`游戏结束,${winner}获胜!`);
return true;
}
return false;
}
function startGame() {
let userScore = 0;
let computerScore = 0;
while (true) {
console.log(`当前比分:用户 ${userScore} : ${computerScore} 电脑`);
const userInput = playRound();
const computerInput = computerRound();
console.log(`电脑选择:${computerInput}`);
if (userInput === computerInput) {
console.log('平局');
} else if (
(userInput === '剪刀' && computerInput === '布') ||
(userInput === '石头' && computerInput === '剪刀') ||
(userInput === '布' && computerInput === '石头')
) {
console.log('你赢了');
userScore++;
} else {
console.log('你输了');
computerScore++;
}
if (checkGameOver(userScore, computerScore)) break; // 检查游戏是否结束
}
}
startGame();
▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰五子棋(简易版)▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰
初步实现电脑下棋逻辑,能够实现简单的防守策略。
// 五子棋
const readlineSync = require('readline-sync');
class Board {
constructor() {
this.board = new Array(9);
for (let i = 0; i < this.board.length; i++) {
this.board[i] = new Array(9);
for (let j = 0; j < this.board.length; j++) {
this.board[i][j] = "+";
}
}
this.blackBoard = "O";
this.whiteBoard = "X";
this.enptyBoard = "+";
}
showBoard() {
console.log(" 0 1 2 3 4 5 6 7 8 9");
for (let i = 0; i < this.board.length; i++) {
let showData = " " + String(i + 1) + " ";
for (let j = 0; j < this.board.length; j++) {
showData += this.board[i][j] + " ";
}
console.log(showData);
}
}
isEmpty(x, y) {
return this.board[x][y] === this.enptyBoard;
}
isBlack(x, y) {
return this.board[x][y] === this.blackBoard;
}
isWhite(x, y) {
return this.board[x][y] === this.whiteBoard;
}
setPiece(x, y, piece) {
this.board[x][y] = piece;
}
}
class Game {
constructor() {
this.board = new Board();
this.player = 1;
this.playerRow = 1;
this.playerCol = 1;
this.cpuRow = 1;
this.cpuCol = 1;
}
playLoop() {
while (true) {
this.board.showBoard();
console.log("当前玩家为:" + (this.player === 1 ? "黑棋 (O)" : "白棋 (X)"));
this.playRound();
if (this.isWin(this.playerRow, this.playerCol)) {
this.board.showBoard();
console.log("恭喜玩家 " + (this.player === 1 ? "黑棋 (O)" : "白棋 (X)") + " 赢得了比赛!");
break;
}
this.shiftRound();
console.clear();
}
}
playRound() {
var row;
var col;
while (true) {
if (this.player === 1) {
console.log("黑棋下棋");
console.log("请输入坐标(例如:12):")
let answer = readlineSync.question("input:");
row = Number(answer[0]) - 1;
col = Number(answer[1]) - 1;
this.playerRow = row;
this.playerCol = col;
} else {
""
console.log("白棋下棋");
[row, col] = this.cpuRound(); // cpu下棋
this.cpuRow = row;
this.cpuCol = col;
}
if (this.isValidCoordinate(row, col) && this.board.isEmpty(row, col)) {
this.board.setPiece(row, col, this.player === 1 ? this.board.blackBoard : this.board.whiteBoard);
break;
} else {
console.log("输入无效,请重新输入。");
}
}
}
cpuRound() {
// 查看横
let finalRow;
let finalCol;
let toright = 1 //向右
let toleft = -1 //向左
let coherent = 1 //连贯
let maxcoherent = 1 //最大连贯
while (this.playerRow + toright < 9 && this.board.isBlack(this.playerRow, this.playerCol + toright)) {
coherent++
toright++
}
while (this.playerRow + toleft >= 0 && this.board.isBlack(this.playerRow, this.playerCol + toleft)) {
coherent++
toleft--
}
if (coherent >= maxcoherent) {
let newCol = this.playerCol + toright;
if (this.isValidCoordinate(this.playerRow, newCol) && this.board.isEmpty(this.playerRow, newCol)) {
finalRow = this.playerRow;
finalCol = newCol;
maxcoherent = coherent;
} else {
newCol = this.playerCol + toleft;
if (this.isValidCoordinate(this.playerRow, newCol) && this.board.isEmpty(this.playerRow, newCol)) {
finalRow = this.playerRow;
finalCol = newCol;
maxcoherent = coherent;
}
}
}
//查看竖
let toup = 1 //向上
let todown = -1 //向下
coherent = 1
while (this.playerRow + toup < 9 && this.board.isBlack(this.playerRow + toup, this.playerCol)) {
coherent++
toup++
}
while (this.playerRow + todown >= 0 && this.board.isBlack(this.playerRow + todown, this.playerCol)) {
coherent++
todown--
}
if (coherent >= maxcoherent) {
let newRow = this.playerRow + toup;
if (this.isValidCoordinate(newRow, this.playerCol) && this.board.isEmpty(newRow, this.playerCol)) {
finalRow = newRow;
finalCol = this.playerCol;
maxcoherent = coherent;
} else {
newRow = this.playerRow + todown;
if (this.isValidCoordinate(newRow, this.playerCol) && this.board.isEmpty(newRow, this.playerCol)) {
finalRow = newRow;
finalCol = this.playerCol;
maxcoherent = coherent;
}
}
}
//查看斜向右下
let torightdown = 1 //向右下
let toleftup = -1 //向左上
coherent = 1
while (this.playerRow + torightdown < 9 && this.playerCol + torightdown < 9 && this.board.isBlack(this.playerRow + torightdown, this.playerCol + torightdown)) {
coherent++
torightdown++
}
while (this.playerRow + toleftup >= 0 && this.playerCol + toleftup >= 0 && this.board.isBlack(this.playerRow + toleftup, this.playerCol + toleftup)) {
coherent++
toleftup--
}
if (coherent >= maxcoherent) {
let newRow = this.playerRow + torightdown;
let newCol = this.playerCol + torightdown;
maxcoherent = coherent;
if (this.isValidCoordinate(newRow, newCol) && this.board.isEmpty(newRow, newCol)) {
finalRow = newRow;
finalCol = newCol;
} else {
newRow = this.playerRow + toleftup;
newCol = this.playerCol + toleftup;
if (this.isValidCoordinate(newRow, newCol) && this.board.isEmpty(newRow, newCol)) {
finalRow = newRow;
finalCol = newCol;
}
}
}
//查看斜向左下
let toleftdown = 1 //向左下
let torightup = -1 //向右上
coherent = 1
while (this.playerRow + toleftdown < 9 && this.playerCol - toleftdown >= 0 && this.board.isBlack(this.playerRow + toleftdown, this.playerCol - toleftdown)) {
coherent++
toleftdown++
}
while (this.playerRow + torightup >= 0 && this.playerCol - torightup < 9 && this.board.isBlack(this.playerRow + torightup, this.playerCol - torightup)) {
coherent++
torightup--
}
if (coherent >= maxcoherent) {
let newRow = this.playerRow + toleftdown;
let newCol = this.playerCol + toleftdown;
if (this.isValidCoordinate(newRow, newCol) && this.board.isEmpty(newRow, newCol)) {
finalRow = newRow;
finalCol = newCol;
maxcoherent = coherent;
} else {
newRow = this.playerRow + torightup;
newCol = this.playerCol + torightup;
if (this.isValidCoordinate(newRow, newCol) && this.board.isEmpty(newRow, newCol)) {
finalRow = newRow;
finalCol = newCol;
maxcoherent = coherent;
}
}
}
return [finalRow, finalCol]
}
shiftRound() {
// 轮流下棋
this.player = this.player === 1 ? 2 : 1;
}
isValidCoordinate(row, col) {
// 判断坐标是否在棋盘范围内
return row >= 0 && row < 9 && col >= 0 && col < 9;
}
isWin(row, col) {
const directions = [
{dx: 0, dy: 1}, // 水平
{dx: 1, dy: 0}, // 垂直
{dx: 1, dy: 1}, // 斜向右下
{dx: 1, dy: -1} // 斜向左下
];
const piece = this.player === 1 ? this.board.blackBoard : this.board.whiteBoard;
for (let dir of directions) {
let count = 1;
let x = row + dir.dx;
let y = col + dir.dy;
while (this.isValidCoordinate(x, y) && this.board.board[x][y] === piece) {
count++;
x += dir.dx;
y += dir.dy;
}
x = row - dir.dx;
y = col - dir.dy;
while (this.isValidCoordinate(x, y) && this.board.board[x][y] === piece) {
count++;
x -= dir.dx;
y -= dir.dy;
}
if (count >= 5) {
return true;
}
}
return false;
}
}
let game = new Game();
game.playLoop();