codeFlowType:
codeWrite
codeFlowLang:
JavaScript
Напиши код игры крестики нолики на c++
Конечно! Вот код игры "Крестики-Нолики" на JavaScript, оформленный с использованием разметки Markdown:
// Game initialization
const board = ["", "", "", "", "", "", "", "", ""];
let currentPlayer = "X";
let gameOver = false;
// Function to render the board
function renderBoard() {
console.clear();
console.log(` ${board[0]} | ${board[1]} | ${board[2]} `);
console.log("---|---|---");
console.log(` ${board[3]} | ${board[4]} | ${board[5]} `);
console.log("---|---|---");
console.log(` ${board[6]} | ${board[7]} | ${board[8]} `);
}
// Function to check for a win
function checkWin() {
const winPatterns = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
return winPatterns.some(pattern =>
board[pattern[0]] === currentPlayer &&
board[pattern[1]] === currentPlayer &&
board[pattern[2]] === currentPlayer
);
}
// Function to handle player moves
function makeMove(position) {
if (board[position] === "" && !gameOver) {
board[position] = currentPlayer;
renderBoard();
if (checkWin()) {
console.log(`Игрок ${currentPlayer} выиграл!`);
gameOver = true;
} else if (board.every(cell => cell)) {
console.log("Ничья!");
gameOver = true;
} else {
currentPlayer = currentPlayer === "X" ? "O" : "X";
}
} else {
console.log("Неверный ход. Попробуйте снова.");
}
}
// Start the game
renderBoard();
// Example usage
makeMove(0); // Player X makes a move at position 0
makeMove(1); // Player O makes a move at position 1
makeMove(4); // Player X makes a move at position 4
makeMove(2); // Player O makes a move at position 2
makeMove(8); // Player X makes a move at position 8
Теперь представлю код игры "Крестики-Нолики" на C++:
#include <iostream>
#include <vector>
using namespace std;
char currentPlayer;
vector<char> board(9, ' ');
void renderBoard() {
system("clear"); // Для Windows используйте "cls"
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "---|---|---" << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "---|---|---" << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
}
bool checkWin() {
const int winPatterns[8][3] = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}
};
for (const auto& pattern : winPatterns) {
if (board[pattern[0]] == currentPlayer &&
board[pattern[1]] == currentPlayer &&
board[pattern[2]] == currentPlayer) {
return true;
}
}
return false;
}
void makeMove(int position) {
if (board[position] == ' ') {
board[position] = currentPlayer;
renderBoard();
if (checkWin()) {
cout << "Игрок " << currentPlayer << " выиграл!" << endl;
} else if (count(board.begin(), board.end(), ' ') == 0) {
cout << "Ничья!" << endl;
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
} else {
cout << "Неверный ход. Попробуйте снова." << endl;
}
}
int main() {
currentPlayer = 'X';
renderBoard();
int move;
while (true) {
cout << "Игрок " << currentPlayer << ", введите номер клетки (1-9): ";
cin >> move;
makeMove(move - 1);
}
return 0;
}
Вы можете копировать и вставлять этот код в соответствующие среда разработки для JavaScript и C++.