作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在控制台中使用 JavaScript(无 DOM)
编写井字棋游戏。 我一开始只做一个玩家游戏,而且只玩“X”。当玩家选择行索引和列索引,然后将其存储在数组(移动)中时,但我被卡住了,玩家只能执行一次移动,仅此而已。我真的不知道接下来该怎么办。
// Variables
let playerName, playerMove = [],
playerShape, boardGame;
// Functions
// Get And Check Name
function getCheckName() {
let name = prompt("Please Enter Your Name");
while (name == null || !isNaN(name) || name == ' ') {
alert("Numbers And Spaces Not Allowed");
name = prompt("Please Enter Your Name");
}
alert("Welcome " + name);
return name;
}
playerName = getCheckName();
playerShape = 'X';
//----------------------------------------------------------------------------------------
// // Get Row And Col
function getRowAndCol() {
let row, col, rowAndColArr = [];
row = parseInt(prompt("Please Enter A Row"));
while (row > 2 || row < 0 || row === null || isNaN(row)) {
alert("Not a Valid Row number");
row = parseInt(prompt("Please Enter A Row"));
}
rowAndColArr[rowAndColArr.length] = row;
col = parseInt(prompt("Please Enter A Col"));
while (col > 2 || col < 0 || col === null || isNaN(col)) {
alert("Not a Valid Col number");
col = parseInt(prompt("Please Enter A Col"));
}
rowAndColArr[rowAndColArr.length] = col;
return rowAndColArr;
}
playerMove = getRowAndCol();
//----------------------------------------------------------------------------------------
// Board Game
boardGame = [
["_", "_", "_"],
["_", "_", "_"],
["_", "_", "_"]
];
function boardFn(board, move) {
for (let rw = 0; rw < board.length; rw++) {
for (let cl = 0; cl < board[rw].length; cl++) {
while (rw === move[0] && cl === move[1] && board[rw][cl] === "_") {
board[rw][cl] = playerShape;
}
}
}
return board;
}
let keepGame = boardFn(boardGame, playerMove);
do {
} while (condition);
console.log(checkWinner());
//----------------------------------------------------------------------------------------
// Check Win
function equal3(a, b, c) {
return (a == b && b == c && a != "_");
};
function checkWinner() {
let winner = null;
// Win In Horizontal
for (let rw = 0; rw < 3; rw++) {
if (equal3(boardGame[rw][0], boardGame[rw][1], boardGame[rw][2])) {
winner = boardGame[rw][0];
alert(playerName + " You Won line");
}
}
// Win In Vertical
for (let cl = 0; cl < 3; cl++) {
if (equal3(boardGame[0][cl], boardGame[1][cl], boardGame[2][cl])) {
winner = boardGame[cl][0];
alert(playerName + " You Won col");
}
}
// Win In Diagonal
if (equal3(boardGame[0][0], boardGame[1][1], boardGame[2][2])) {
winner = boardGame[0][0];
alert(playerName + " You Won diagonal");
}
// Win In Diagonal (Other Way)
if (equal3(boardGame[2][0], boardGame[1][1], boardGame[0][2])) {
winner = boardGame[2][0];
alert(playerName + " You Won digonal other");
}
return winner;
}
最佳答案
Updated code for two players
var board = [
["_", "_", "_"],
["_", "_", "_"],
["_", "_", "_"]
];
//This function will prompt and get user name
function getName(player) {
let name = prompt("Please Enter " + player + " Name");
while (name == null || !isNaN(name) || name == ' ') {
alert("Numbers And Spaces Not Allowed");
name = prompt("Please Enter " + player + " Name");
}
alert("Welcome, " + name + "!");
return name;
}
//This function will prompt and get row and column from player
function getRowAndCol(player) {
let row, col, rowAndColArr = [];
row = parseInt(prompt(player + "! Please Enter A Row"));
while (row > 2 || row < 0 || row === null || isNaN(row)) {
alert("Not a Valid Row number");
row = parseInt(prompt(player + "! Please Enter A Row"));
}
rowAndColArr[rowAndColArr.length] = row;
col = parseInt(prompt(player + "! Please Enter A Col"));
while (col > 2 || col < 0 || col === null || isNaN(col)) {
alert("Not a Valid Col number");
col = parseInt(prompt(player + "! Please Enter A Col"));
}
rowAndColArr[rowAndColArr.length] = col;
//This block will check if the given box already marked. If so it will prompt for the move.
if (board[rowAndColArr[0]][rowAndColArr[1]] !== "_") {
alert("Already marked on the given box. Please Enter different row and column.!");
return getRowAndCol(player)
}
return rowAndColArr;
}
//This function will update the position with the given shape according to the move
function boardFn(board, move, shape) {
for (let rw = 0; rw < board.length; rw++) {
for (let cl = 0; cl < board[rw].length; cl++) {
while (rw === move[0] && cl === move[1] && board[rw][cl] === "_") {
board[rw][cl] = shape;
}
}
}
}
//This function will check if it is tie
function isTie() {
for (let rw = 0; rw < board.length; rw++) {
for (let cl = 0; cl < board[rw].length; cl++) {
if( board[rw][cl] === "_" ) {
return false;
}
}
}
return true;
}
//These functions will check whether the current player win or not
function equal3(a, b, c) {
return (a == b && b == c && a != "_");
};
function checkWinner(player) {
let winner = null;
// Win In Horizontal
for (let rw = 0; rw < 3; rw++) {
if (equal3(board[rw][0], board[rw][1], board[rw][2])) {
winner = board[rw][0];
alert(player + " You Won line");
}
}
// Win In Vertical
for (let cl = 0; cl < 3; cl++) {
if (equal3(board[0][cl], board[1][cl], board[2][cl])) {
winner = board[cl][0];
alert(player + " You Won col");
}
}
// Win In Diagonal
if (equal3(board[0][0], board[1][1], board[2][2])) {
winner = board[0][0];
alert(player + " You Won diagonal");
}
// Win In Diagonal (Other Way)
if (equal3(board[2][0], board[1][1], board[0][2])) {
winner = board[2][0];
alert(player + " You Won digonal other");
}
return winner;
}
var player1 = getName("Player 1") // getting player 1 name
alert(player1 + "! Your shape is 'X'");
var player2 = getName("Player 2") // getting player 2 name
alert(player2 + "! Your shape is 'O'");
// Variables to maintain current players and shape
var currentPlayer = ""
var currentShape = ""
// This block will get and validate moves until we get a winner
while (true) {
// This block will switch player after a move
if (currentPlayer == "") {
currentPlayer = player1;
currentShape = "X";
} else if (currentPlayer == player1) {
currentPlayer = player2;
currentShape = "O";
} else {
currentPlayer = player1;
currentShape = "X";
}
var move = getRowAndCol(currentPlayer);
boardFn(board, move, currentShape);
var status = "";
for (var i in board) {
for (var j in board[i]) {
status += board[i][j] + " ";
}
status += "\n";
}
console.log(status);
console.log("-------------------------------");
if (checkWinner(currentPlayer)) {
break;
} else if (isTie()) {
alert("Game tied.!")
break;
}
}
关于javascript - 井字棋如何让游戏继续进行直到获胜或平局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58341420/
我是一名优秀的程序员,十分优秀!