Tic-tac-toe

Description

In the game of tic-tac-toe, two players take turns marking squares of an initially empty 3 × 3 grid with either X’s or O’s. The first player always marks squares using X’s, whereas the second player always marks squares using O’s. If at any point during the game either player manages to mark three consecutive squares in a row, column, or diagonal with his/her symbol, the game terminates.

Given a board configuration, your goal is to determine whether the board configuration represents the possible final state of a valid tic-tac-toe game.

Input

The input test file will contain multiple cases. Each test case consists of a single line containing 9 characters, which represent the 9 squares of a tic-tac-toe grid, given one row at a time. Each character on the line will either be ‘X’, ‘O’ (the letter O), or ‘.’ (indicating an unfilled square). The end-of-file is marked by a single line containing the word “end”.

Output

For each input test case, write a single line containing either the word “valid” or “invalid” indicating whether the given board configuration is the final state of some possible tic-tac-toe game.

Sample Input

XXXOO.XXX
XOXOXOXOX
OXOXOXOXO
XXOOOXXOX
XO.OX...X
.XXX.XOOO
X.OO..X..
OOXXXOOXO
end

Sample Output

invalid
valid
invalid
valid
valid
invalid
invalid
invalid 

题意:
For each input test case, write a single line containing either the word “valid” or “invalid” indicating whether the given board configuration is the final state of some possible tic-tac-toe game.

1. A赢:A符合赢的条件(某行或某列或对角线的字符一样),且B不符合赢的条件,且A的步数比B多1,因为A先走的。
2. B赢:B符合赢的条件,且A不符合赢的条件,且A跟B的步数一样。
3. 平局:A和B都不符合赢的条件,且A的步数为5,B的步数为4.(满格,其他数字组合的满格不符合规则)
4.剩下的:illegal

#include
#include

using namespace std;

int judge_row(char a[][3], char ch)
{
	if ((a[0][0] == ch && a[0][1] == ch && a[0][2] == ch) || (a[1][0] == ch && a[1][1] == ch && a[1][2] == ch) || (a[2][0] == ch && a[2][1] == ch && a[2][2] == ch))
		return 1;
	else return 0;
}

int judge_col(char a[][3], char ch)
{
	if ((a[0][0] == ch && a[1][0] == ch && a[2][0] == ch) || (a[0][1] == ch && a[1][1] == ch && a[2][1] == ch) || (a[0][2] == ch && a[1][2] == ch &&a[2][2] == ch))
		return 1;
	else return 0;
}

int judge_dia(char a[][3], char ch)
{
	if ((a[0][0] == ch && a[1][1] == ch && a[2][2] == ch) || (a[0][2] == ch && a[1][1] == ch && a[2][0] == ch))
		return 1;
	else return 0;
}

int judge(char a[][3], char ch)
{
	if (judge_row(a, ch) || judge_col(a, ch) || judge_dia(a, ch))
		return 1;
	else return 0;
}
int main()
{
	while (true)
	{
		char a[3][3];
		int count_x = 0, count_o = 0;

		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				cin >> a[i][j];
				if (a[i][j] == 'd')
					return 0;
				if (a[i][j] == 'X')
					count_x++;
				if (a[i][j] == 'O')
					count_o++;
			}
		}
		
		if ((judge(a, 'X') == 1) && (judge(a, 'O') == 0) && count_x - 1 == count_o)
			cout << "valid" << endl;
		else if ((judge(a, 'O') == 1) && (judge(a, 'X') == 0) && count_x == count_o)
			cout << "valid" << endl;
		else if ((judge(a, 'O') == 0) && (judge(a, 'X') == 0) && count_x - 1 == count_o &&  count_x + count_o == 9)
			cout << "valid" << endl;
		else cout << "invalid" << endl;
	}

}

你可能感兴趣的:(C)