在C ++编码井字游戏中需要帮助 [英] Need help in c++ coding tictac toe game
本文介绍了在C ++编码井字游戏中需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "GUI.cpp"
using namespace std;
void display( char board[3][3][3][3] )
{
for( int R=0; R<3; R++ )
{
for( int r=0; r<3; r++ )
{
for( int C=0; C<3; C++ )
{
for( int c=0; c<3; c++ )
{
setColor(RGBI(1,0,0,1),RGBI(1,1,0,1));
cout << board[R][C][r][c];
setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
}
cout <<" ";
}
cout << endl;
}
cout << endl;
}
setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
cout <<"Click on ";
setColor(RGBI(1,0,0,1),RGBI(1,1,0,1));
cout <<"highlighted cell";
setColor(RGBI(1,1,1,0),RGBI(0,0,0,0));
cout <<" above using the [Mouse]"<< endl;
}
bool mouseToBoardCoord( MouseClick const& m, int& R, int& C, int& r, int& c )
{
// Player clicked on first big row?
if( (0 <= m.x) && (m.x < 3) && (0 <= m.y) && (m.y < 3) )
{
R = 0;
C = 0;
if ( m.x == 0 )c = 0;
else if( m.x == 1 )c = 1;
else if( m.x == 2 )c = 2;
if ( m.y == 0 )r = 0;
else if( m.y == 1 )r = 1;
else if( m.y == 2 )r = 2;
}
else if( (4 <= m.x) && (m.x < 7) && (0 <= m.y) && (m.y < 3) )
{
R = 0;
C = 1;
if ( m.x == 4 )c = 0;
else if( m.x == 5 )c = 1;
else if( m.x == 6 )c = 2;
if ( m.y == 0 )r = 0;
else if( m.y == 1 )r = 1;
else if( m.y == 2 )r = 2;
}
else if( (8 <= m.x) && (m.x < 11) && (0 <= m.y) && (m.y < 3) )
{
R = 0;
C = 2;
if ( m.x == 8 )c = 0;
else if( m.x == 9 )c = 1;
else if( m.x == 10 )c = 2;
if ( m.y == 0 )r = 0;
else if( m.y == 1 )r = 1;
else if( m.y == 2 )r = 2;
}
// Player clicked on second big row?
else if( (0 <= m.x) && (m.x < 3) && (4 <= m.y) && (m.y < 7) )
{
R = 1;
C = 0;
if ( m.x == 0 )c = 0;
else if( m.x == 1 )c = 1;
else if( m.x == 2 )c = 2;
if ( m.y == 4 )r = 0;
else if( m.y == 5 )r = 1;
else if( m.y == 6 )r = 2;
}
else if( (4 <= m.x) && (m.x < 7) && (4 <= m.y) && (m.y < 7) )
{
R = 1;
C = 1;
if ( m.x == 4 )c = 0;
else if( m.x == 5 )c = 1;
else if( m.x == 6 )c = 2;
if ( m.y == 4 )r = 0;
else if( m.y == 5 )r = 1;
else if( m.y == 6 )r = 2;
}
else if( (8 <= m.x) && (m.x < 11) && (4 <= m.y) && (m.y < 7) )
{
R = 1;
C = 2;
if ( m.x == 8 )c = 0;
else if( m.x == 9 )c = 1;
else if( m.x == 10 )c = 2;
if ( m.y == 4 )r = 0;
else if( m.y == 5 )r = 1;
else if( m.y == 6 )r = 2;
}
// Player clicked on third big row?
else if( (0 <= m.x) && (m.x < 3) && (8 <= m.y) && (m.y < 11) )
{
R = 2;
C = 0;
if ( m.x == 0 )c = 0;
else if( m.x == 1 )c = 1;
else if( m.x == 2 )c = 2;
if ( m.y == 8 )r = 0;
else if( m.y == 9 )r = 1;
else if( m.y == 10 )r = 2;
}
else if( (4 <= m.x) && (m.x < 7) && (8 <= m.y) && (m.y < 11) )
{
R = 2;
C = 1;
if ( m.x == 4 )c = 0;
else if( m.x == 5 )c = 1;
else if( m.x == 6 )c = 2;
if ( m.y == 8 )r = 0;
else if( m.y == 9 )r = 1;
else if( m.y == 10 )r = 2;
}
else if( (8 <= m.x) && (m.x < 11) && (8 <= m.y) && (m.y < 11) )
{
R = 2;
C = 2;
if ( m.x == 8 )c = 0;
else if( m.x == 9 )c = 1;
else if( m.x == 10 )c = 2;
if ( m.y == 8 )r = 0;
else if( m.y == 9 )r = 1;
else if( m.y == 10 )r = 2;
}
// Player clicked outside our 9x9 board?
else
{
return false;
}
// Player clicked inside our 9x9 board
return true;
}
int main()
{
char board[3][3][3][3] = {};
bool FirstTime = true;
int prev_r;
int prev_c;
while(true)
{
system("CLS");
display(board);
MouseClick m = getMouseClick();
int R,C,r,c;
if( mouseToBoardCoord(m,R,C,r,c) )
{
//
// neeed help in this section
//
if (FirstTime)
{
board[R][C][r][c] = 'X';
FirstTime = false;
prev_r = r;
prev_c = c;
}
else
{
if ( R == prev_r && C == prev_c)
{
board[R][C][r][c] = 'o';
prev_r = r;
prev_c = c;// need help at here!!
}
}
}
}
}
我不知道如何为替代X和O以及胜利/失败/平局
编码
希望尽快得到您的答复,并对此做一些解释.真的非常感谢
i dont knw how to code for the alternative X and O and for the win / lose/ tie
hope to get ur answer as soon as possible and some explanation on it. really very appreciate
推荐答案
在这样的游戏中,如果计算两个用户的动作,那么(假设我们从1开始)
- 如果
count
为奇数,则当前移动为''X''
一个.
In such a game, if count the moves of both users then (suppose we start with 1)
- if
count
is odd then the current move is an''X''
one.
- 如果
count
是偶数,则当前移动是''O''
一个.
- if
count
is even then the current move is an''O''
one.
这篇关于在C ++编码井字游戏中需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文