当前位置:   article > 正文

Tic-tac-toe

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

  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. int judge_row(char a[][3], char ch)
  5. {
  6. 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))
  7. return 1;
  8. else return 0;
  9. }
  10. int judge_col(char a[][3], char ch)
  11. {
  12. 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))
  13. return 1;
  14. else return 0;
  15. }
  16. int judge_dia(char a[][3], char ch)
  17. {
  18. 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))
  19. return 1;
  20. else return 0;
  21. }
  22. int judge(char a[][3], char ch)
  23. {
  24. if (judge_row(a, ch) || judge_col(a, ch) || judge_dia(a, ch))
  25. return 1;
  26. else return 0;
  27. }
  28. int main()
  29. {
  30. while (true)
  31. {
  32. char a[3][3];
  33. int count_x = 0, count_o = 0;
  34. for (int i = 0; i < 3; i++)
  35. {
  36. for (int j = 0; j < 3; j++)
  37. {
  38. cin >> a[i][j];
  39. if (a[i][j] == 'd')
  40. return 0;
  41. if (a[i][j] == 'X')
  42. count_x++;
  43. if (a[i][j] == 'O')
  44. count_o++;
  45. }
  46. }
  47. if ((judge(a, 'X') == 1) && (judge(a, 'O') == 0) && count_x - 1 == count_o)
  48. cout << "valid" << endl;
  49. else if ((judge(a, 'O') == 1) && (judge(a, 'X') == 0) && count_x == count_o)
  50. cout << "valid" << endl;
  51. else if ((judge(a, 'O') == 0) && (judge(a, 'X') == 0) && count_x - 1 == count_o && count_x + count_o == 9)
  52. cout << "valid" << endl;
  53. else cout << "invalid" << endl;
  54. }
  55. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/261890
推荐阅读
相关标签
  

闽ICP备14008679号