random - 它在 c++ 中显示以下错误。[错误] 无效初始化中函数“int rand()”的参数过多
问题描述
// A C++ Program to play tic-tac-toe
#include<iostream>
using namespace std;
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define COMPUTER 1
#define HUMAN 2
#define SIDE 3 // Length of the board
// Computer will move with 'O'
// and human with 'X'
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
class game
{
int i,j;
public:void showBoard(char board[][SIDE]);
void showInstructions();
void initialise(char board[][SIDE], int moves[]);
void declareWinner(int whoseTurn);
bool rowCrossed(char board[][SIDE]);
bool columnCrossed(char board[][SIDE]);
bool diagonalCrossed(char board[][SIDE]);
bool gameOver(char board[][SIDE]);
void playTicTacToe(int whoseTurn);
};
// A function to show the current board status
void game::showBoard(char board[][SIDE])
{
cout<<"\n\n";
cout<<"\t\t\t %c | %c | %c \n", board[0][0],
board[0][1], board[0][2];
cout<<"\t\t\t--------------\n";
cout<<"\t\t\t %c | %c | %c \n", board[1][0],
board[1][1], board[1][2];
cout<<"\t\t\t--------------\n";
cout<<"\t\t\t %c | %c | %c \n\n", board[2][0],
board[2][1], board[2][2];
return;
};
// A function to show the instructions
void game::showInstructions()
{
cout<<"\t\t\t Tic-Tac-Toe\n\n";
cout<<"Choose a cell numbered from 1 to 9 as below"
" and play\n\n";
cout<<"\t\t\t 1 | 2 | 3 \n";
cout<<"\t\t\t--------------\n";
cout<<"\t\t\t 4 | 5 | 6 \n";
cout<<"\t\t\t--------------\n";
cout<<"\t\t\t 7 | 8 | 9 \n\n";
cout<<"-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n";
return;
}
// A function to initialise the game
void game::initialise(char board[][SIDE],int moves[])
{
int i,j;
// Initiate the random number generator so that
// the same configuration doesn't arises
// Initially the board is empty
for ( i=0; i<SIDE; i++)
{
for ( j=0; j<SIDE; j++)
board[i][j] = ' ';
}
// Fill the moves with numbers
for ( i=0; i<SIDE*SIDE; i++)
moves[i] = i;
// randomise the moves
rand(moves, moves + SIDE*SIDE);
return;
}
// A function to declare the winner of the game
void game::declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
cout<<"COMPUTER has won\n";
else
cout<<"HUMAN has won\n";
return;
}
// A function that returns true if any of the row
// is crossed with the same player's movet
bool rowCrossed(char board[][SIDE])
{ int i;
for ( i=0; i<SIDE; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != ' ')
return (true);
}
return(false);
}
// A function that returns true if any of the column
// is crossed with the same player's move
bool columnCrossed(char board[][SIDE])
{ int i;
for ( i=0; i<SIDE; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != ' ')
return (true);
}
return(false);
}
// A function that returns true if any of the diagonal
// is crossed with the same player's move
bool diagonalCrossed(char board[][SIDE])
项目清单
{ if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') 返回(真的);
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') return(true); return(false);
}
// 一个函数,如果游戏结束则返回 true // 否则返回 false bool gameOver(char board[][SIDE]) { return(rowCrossed(board) || columnCrossed(board) ||diagonalCrossed(board) ) ; }
// 一个玩井字游戏的函数 void game::playTicTacToe(int whoTurn) {
// A 3*3 Tic-Tac-Toe board for playing char board[SIDE][SIDE]; int moves[SIDE*SIDE]; // Initialise the game initialise(board, moves); // Show the instructions before playing showInstructions(); int moveIndex = 0, x, y; // Keep playing till the game is over or it is a draw while (gameOver(board) == false && moveIndex != SIDE*SIDE) { if (whoseTurn == COMPUTER) { x = moves[moveIndex] / SIDE; y = moves[moveIndex] % SIDE; board[x][y] = COMPUTERMOVE; cout<<"COMPUTER has put a %c in cell %d\n", COMPUTERMOVE, moves[moveIndex]+1; showBoard(board); moveIndex ++; whoseTurn = HUMAN; } else if (whoseTurn == HUMAN) { x = moves[moveIndex] / SIDE; y = moves[moveIndex] % SIDE; board[x][y] = HUMANMOVE; printf ("HUMAN has put a %c in cell %d\n", HUMANMOVE, moves[moveIndex]+1); showBoard(board); moveIndex ++; whoseTurn = COMPUTER; } } // If the game has drawn if (gameOver(board) == false && moveIndex == SIDE * SIDE) printf("It's a draw\n"); else { // Toggling the user to declare the actual // winner if (whoseTurn == COMPUTER) whoseTurn = HUMAN; else if (whoseTurn == HUMAN) whoseTurn = COMPUTER; // Declare the winner declareWinner(whoseTurn); } return;
}
// 驱动程序 int main() { // 让我们先从 COMPUTER 开始玩游戏 playTicTacToe(COMPUTER); srand(时间(NULL));
return (0);
}
解决方案
推荐阅读
- python-3.x - 用 pdfrw 填充 PDF 不显示值
- gstreamer - 在 gstreamer 中禁用调试日志?
- javascript - 如何允许多个按钮在具有不同文本的同时占用 div 内的所有空间?
- linux - 如何在自定义路径中安装 elasticsearch?
- node.js - 如何在heroku中有超过30秒的响应超时
- node.js - 基于多个文件静态生成类型文件
- python - pyomo在定义目标函数时可以处理分割函数(由numpy定义)吗?
- c++ - 如何在 c++ 中使用 cout 中的录音
- python - 每次调用 train.dataset 时输入数字都会增加
- docker - 是否将 Nextcloud 的最佳选择列入白名单