이 프로그램은 두 명의 플레이어가 번갈아 가며 자신의 이동(행과 열)을 입력하여 틱택토를 플레이할 수 있습니다. 각 이동 후 승리와 무승부를 확인하고 보드를 표시합니다. 게임은 한 플레이어가 이기거나 게임이 무승부로 끝날 때까지 계속됩니다.
게임 작동 방식은 다음과 같습니다.
1. 프로그램이 빈 틱택토 보드를 표시합니다.
2. 플레이어는 행과 열을 지정하여 번갈아 가며 자신의 수를 입력합니다(예: 왼쪽 상단 모서리의 경우 "1 1").
3. 프로그램은 이동이 유효한지 확인하고 보드를 업데이트합니다.
4. 각 이동 후 게임이 끝났는지(승리 또는 무승부) 확인합니다.
5. 플레이어가 이기면 이긴 보드와 이긴 플레이어가 표시됩니다.
6. 게임은 승리 또는 무승부가 발생할 때까지 계속되고 그 후 종료됩니다.
이 코드를 C++ 개발 환경에서 실행하여 두 명의 플레이어와 함께 게임을 플레이할 수 있습니다.
#include <iostream>
#include <vector>
using namespace std;
// Function to print the Tic-Tac-Toe board
void printBoard(const vector<vector<char>>& board) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << board[i][j];
if (j < 2) {
cout << " | ";
}
}
cout << endl;
if (i < 2) {
cout << "---------" << endl;
}
}
}
// Function to check if the game is over (win or draw)
bool isGameOver(const vector<vector<char>>& board, char player) {
// Check rows, columns, and diagonals
for (int i = 0; i < 3; ++i) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true; // Row win
}
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true; // Column win
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true; // Diagonal win (top-left to bottom-right)
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true; // Diagonal win (top-right to bottom-left)
}
// Check for a draw (all cells filled)
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == ' ') {
return false; // Game is not over yet
}
}
}
return true; // It's a draw
}
int main() {
vector<vector<char>> board(3, vector<char>(3, ' '));
char currentPlayer = 'X';
int row, col;
cout << "Tic-Tac-Toe Game" << endl;
while (true) {
cout << "Current board:" << endl;
printBoard(board);
cout << "Player " << currentPlayer << ", enter your move (row and column): ";
cin >> row >> col;
if (row < 1 || row > 3 || col < 1 || col > 3 || board[row - 1][col - 1] != ' ') {
cout << "Invalid move. Try again." << endl;
continue;
}
board[row - 1][col - 1] = currentPlayer;
if (isGameOver(board, currentPlayer)) {
cout << "Player " << currentPlayer << " wins!" << endl;
printBoard(board);
break;
}
// Switch to the other player
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
cout << "Game over. Thanks for playing!" << endl;
return 0;
}
결과 값
Tic-Tac-Toe Game
Current board:
| |
---------
| |
---------
| |
Player X, enter your move (row and column): 1 1
Current board:
X | |
---------
| |
---------
| |
Player O, enter your move (row and column): 2 2
Current board:
X | |
---------
| O |
---------
| |
Player X, enter your move (row and column): 1 2
Current board:
X | X |
---------
| O |
---------
| |
Player O, enter your move (row and column): 2 1
Current board:
X | X |
---------
O | O |
---------
| |
Player X, enter your move (row and column): 3 3
Current board:
X | X |
---------
O | O |
---------
| | X
Player X wins!
X | X |
---------
O | O |
---------
| | X
Game over. Thanks for playing!
이 예에서는 두 명의 플레이어(X와 O)가 번갈아 가며 이동합니다. 플레이어 X가 대각선 X를 만들어 승리하면 게임이 종료됩니다. 프로그램이 승리한 보드와 "Game over" 메시지를 표시합니다.
'Programming language > C++' 카테고리의 다른 글
[C++] 텍스트 기반 RPG 구현하기 (0) | 2023.10.06 |
---|---|
[C++] 온도 변환기 구현 (0) | 2023.10.06 |
[C++]미로 생성기 & 미로 풀이 도구 만들기 (1) | 2023.10.06 |
[C++] 파일 암호화 및 복호화 프로그램 소스 코드 (0) | 2023.10.06 |
[C++] 소수 판별 프로그램 (0) | 2023.10.06 |