Từ 1 tới 4 trên tổng số 4 kết quả

Đề tài: The simple program Tick Tack Toe...

  1. #1
    Ngày gia nhập
    12 2012
    Bài viết
    7

    Mặc định The simple program Tick Tack Toe...

    Hi everybody and now I have a very very simple program (It's called "Tick Tack Toe"). But it's very funny !!!

    "There are nine small squares, and they are filled out by numbers (in order 0-9). You'll need to first, choose your game precedence. If you choose "Y", that option means the computer is allowed to play first. Otherwise, the precedence is... you.
    There isn't any human vs human game mode. Instead of this there is only the "Human vs CPU" mode here.

    Let's enter your choice in order 0 - 9. If your choice is valid, then the machine will put your choice (X) to the board item index you specified. (Replaces the nIndex element with the character "X" or "O")

    The "CPU machine" certainly is pretty intelligent. Be careful, it has some tricks that can trim you down immediately!
    When the board is full (All elements are filled by ('X' - 'O')) -> Game draw

    If the game detects there is a valid column, or row, or diagonally line of "X" - "O", then certainly, the game is over.
    IF THE WINNER IS "O", you lose!!
    IF THE WINNER IS "X", you win!!"
    How do you fell about this?

  2. #2
    Ngày gia nhập
    12 2012
    Bài viết
    7

    Here is the code :
    C++ Code:
    1. #include <iostream>
    2. #include <time.h>
    3. using namespace std;
    4. char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
    5.  
    6. int checkwin();
    7. void board();
    8. void Think(bool Player);
    9. bool SetItem(int nNumber, int mark);
    10. bool IsEmpty(int nIndex);
    11. bool IsPlayer(bool Player, int nIndex);
    12. int ThinkColumn(bool Player, int nColumn);
    13. int ThinkRow(bool Player, int nRow);
    14. int ThinkOther(bool Player, int nLine);
    15. int ThinkSpecificItem(bool Player, int nIndex);
    16.  
    17. #define X 1
    18. #define Y 0
    19.  
    20. bool SetItem(int nNumber, int mark){
    21. if(square[nNumber] != nNumber + '0' || nNumber < 1 || nNumber > 9)return false;
    22. square[nNumber] = mark;return true;}
    23.  
    24. bool IsEmpty(int nIndex){return (square[nIndex] == '0' + nIndex);}
    25. bool IsPlayer(bool Player, int nIndex){return (square[nIndex] == ((Player == X) ? 'X' : 'O'));}
    26.  
    27. int ThinkColumn(bool Player, int nColumn){nColumn--;nColumn *= 3;//Try calculating this expression (E.g : 1;2;3)
    28.     int nItems = 0;
    29.     if(IsPlayer(Player, nColumn + 1) == true)nItems++;
    30.     if(IsPlayer(Player, nColumn + 2) == true)nItems++;
    31.     if(IsPlayer(Player, nColumn + 3) == true)nItems++;
    32.  
    33. if(nItems == 2)
    34. {
    35.     if(IsEmpty(nColumn + 1) == true)return nColumn + 1;
    36.     if(IsEmpty(nColumn + 2) == true)return nColumn + 2;
    37.     if(IsEmpty(nColumn + 3) == true)return nColumn + 3;
    38.  
    39. }
    40. return 0;
    41. }
    42.  
    43. int ThinkRow(bool Player, int nRow){
    44.     int nItems = 0;
    45.     if(IsPlayer(Player, nRow + 0) == true)nItems++;
    46.     if(IsPlayer(Player, nRow + 3) == true)nItems++;
    47.     if(IsPlayer(Player, nRow + 6) == true)nItems++;
    48.  
    49. if(nItems == 2)
    50. {
    51.     if(IsEmpty(nRow + 0) == true)return nRow + 0;
    52.     if(IsEmpty(nRow + 3) == true)return nRow + 3;
    53.     if(IsEmpty(nRow + 6) == true)return nRow + 6;
    54. }
    55. return 0;
    56. }
    57.  
    58. int ThinkOther(bool Player, int nLine){
    59. int nItems = 0;
    60. if(nLine == 1){
    61.     if(IsPlayer(Player, 1) == true)nItems++;
    62.     if(IsPlayer(Player, 5) == true)nItems++;
    63.     if(IsPlayer(Player, 9) == true)nItems++;
    64. }
    65. else if(nLine == 2){
    66.     if(IsPlayer(Player, 3) == true)nItems++;
    67.     if(IsPlayer(Player, 5) == true)nItems++;
    68.     if(IsPlayer(Player, 7) == true)nItems++;
    69. }
    70.  
    71.  
    72. if(nItems == 2) //There are two player pieces
    73. {
    74. if(nLine == 1){
    75.     if(IsEmpty(1) == true)return 1;
    76.     if(IsEmpty(5) == true)return 5;
    77.     if(IsEmpty(9) == true)return 9;
    78. }
    79. else if(nLine == 2){
    80.     if(IsEmpty(3) == true)return 3;
    81.     if(IsEmpty(5) == true)return 5;
    82.     if(IsEmpty(7) == true)return 7;
    83. }
    84. }
    85. return 0;
    86. }
    87.  
    88. int ThinkSpecificItem(bool Player, int nIndex)
    89. {
    90. if(IsPlayer(Player, nIndex))
    91. {
    92. if(nIndex == 2 || nIndex == 8)
    93. {
    94.     if(IsEmpty(4) == true && (nIndex == 2 && IsEmpty(1) == true) || (nIndex == 8 && IsEmpty(7) == true))return 4;
    95.     if(IsEmpty(6) == true && (nIndex == 2 && IsEmpty(3) == true) || (nIndex == 8 && IsEmpty(9) == true))return 6;
    96.     if(IsEmpty(5) == true)return 5;
    97. }
    98. else if(nIndex == 4 || nIndex == 6)
    99. {
    100.     if(IsEmpty(2) == true && (nIndex == 4 && IsEmpty(1) == true) || (nIndex == 6 && IsEmpty(3) == true))return 2;
    101.     if(IsEmpty(8) == true && (nIndex == 4 && IsEmpty(7) == true) || (nIndex == 6 && IsEmpty(9) == true))return 8;
    102.     if(IsEmpty(5) == true)return 5;
    103. }
    104. else if(nIndex == 1)
    105. {
    106.     if(IsPlayer(Player, 5) == true && IsEmpty(3) == true && (IsEmpty(2) == true || IsEmpty(9) == true))return 3;
    107.     if(IsEmpty(2) == true && IsEmpty(3) == true)return 2;
    108.     if(IsEmpty(4) == true && IsEmpty(7) == true)return 4;
    109.     if(IsEmpty(5) == true)return 5;
    110. }
    111. else if(nIndex == 3)
    112. {
    113.     if(IsPlayer(Player, 5) == true && IsEmpty(1) == true && (IsEmpty(2) == true || IsEmpty(7) == true))return 1;
    114.     if(IsEmpty(2) == true && IsEmpty(1) == true)return 2;
    115.     if(IsEmpty(6) == true && IsEmpty(9) == true)return 6;
    116.     if(IsEmpty(5) == true)return 5;
    117. }
    118. else if(nIndex == 7)
    119. {
    120.     if(IsPlayer(Player, 5) == true && IsEmpty(9) == true && (IsEmpty(8) == true || IsEmpty(3) == true))return 9;
    121.     if(IsEmpty(4) == true && IsEmpty(1) == true)return 4;
    122.     if(IsEmpty(8) == true && IsEmpty(9) == true)return 8;
    123.     if(IsEmpty(5) == true)return 5;
    124. }
    125. else if(nIndex == 9)
    126. {
    127.     if(IsPlayer(Player, 5) == true && IsEmpty(7) == true && (IsEmpty(8) == true || IsEmpty(1) == true))return 7;
    128.     if(IsEmpty(6) == true && IsEmpty(3) == true)return 6;
    129.     if(IsEmpty(8) == true && IsEmpty(7) == true)return 8;
    130.     if(IsEmpty(5) == true)return 5;
    131. }
    132. }
    133. return 0;
    134. }
    135.  
    136. void Think(bool Player)
    137. {
    138.     char mark = (Player == X) ? 'X' : 'O';
    139.     bool bResult;
    140.     int choice;
    141.     bool Enemy = !Player;
    142. ///////////////Method 1...////////////////////////////////////////////////////////
    143.     if((choice = ThinkOther(Player, 1)) != 0){square[choice] = mark;return;}
    144.     if((choice = ThinkOther(Player, 2)) != 0){square[choice] = mark;return;}
    145.     for(int b = 1; b <= 3; b++){
    146.     if((choice = ThinkColumn(Player, b)) != 0){square[choice] = mark;return;}
    147.     if((choice = ThinkRow(Player, b)) != 0){square[choice] = mark;return;}}
    148.     if((choice = ThinkOther(Enemy, 1)) != 0){square[choice] = mark;return;}
    149.     if((choice = ThinkOther(Enemy, 2)) != 0){square[choice] = mark;return;}
    150.  
    151. ///////////////Method 2...////////////////////////////////////////////////////////
    152.     bResult = (IsPlayer(Player, 2) == true && IsPlayer(Player, 4) == true);
    153.     if(bResult == true && IsEmpty(1) == true){square[1] = mark;return;}
    154.     bResult = (IsPlayer(Player, 2) == true && IsPlayer(Player, 6) == true);
    155.     if(bResult == true && IsEmpty(3) == true){square[3] = mark;return;}
    156.     bResult = (IsPlayer(Player, 4) == true && IsPlayer(Player, 8) == true);
    157.     if(bResult == true && IsEmpty(7) == true){square[7] = mark;return;}
    158.     bResult = (IsPlayer(Player, 6) == true && IsPlayer(Player, 8) == true);
    159.     if(bResult == true && IsEmpty(7) == true){square[9] = mark;return;}
    160.  
    161. ///////////////Method 3...////////////////////////////////////////////////////////
    162.     for(int a = 1; a <= 3; a++){
    163.         if((choice = ThinkColumn(Enemy, a)) != 0){square[choice] = mark;return;}
    164.         if((choice = ThinkRow(Enemy, a)) != 0){square[choice] = mark;return;}}
    165.  
    166. ///////////////Method 4...////////////////////////////////////////////////////////
    167.     for(int c = 1; c <= 9; c++)
    168.         if((choice = ThinkSpecificItem(Player, c)) != 0){square[choice] = mark;return;}
    169.  
    170. ///////////////Method 5...////////////////////////////////////////////////////////
    171. do choice = rand() % 9 + 1;while(IsEmpty(choice) == false);square[choice] = mark;
    172. }
    173. //////////////////////////////////////////////////////////////////////////////////
    174. //////////////////////////////////////////////////////////////////////////////////
    175. int main()
    176. {
    177.     srand(time(NULL));
    178.     bool player = X;
    179.     char mark;
    180.     int i, choice;
    181.     cout << "Computer first? (Press Y) : ";
    182.     cin >> mark;
    183.     if(mark == 'y' || mark == 'Y')player = Y;
    184.     system("cls");
    185. //////////////////////////////////////////////////////////////////////////////////
    186. do
    187. {
    188.     board();
    189.     mark = (player == X) ? 'X' : 'O';
    190.     if(player == X)
    191.     {
    192.         cout << "Player " << player << " ("<< mark <<"), please enter a number : ";
    193.         cin >> choice;
    194.         if(SetItem(choice, mark) == false){
    195.     cout<<"Invalid move ";
    196.     player = !player; //mark = (player == 1) ? 'O' : 'X';
    197.     cin.ignore();
    198.     cin.get();
    199. }
    200. }
    201. else Think(player); //Computer
    202.  
    203.  
    204.  
    205.     i = checkwin(); //check
    206.  
    207.     player = !player;
    208. }while(i == -1);
    209. //////////////////////////////////////////////////////////////////////////////////
    210. board();
    211. if(i == 1) cout<<"==>\a Player "<< ((player == X) ? 'O' : 'X') <<" win ";
    212.  
    213.  
    214. return 0;
    215. }
    216.  
    217. //////////////////////////////////////////////////////////////////////////////////
    218. //////////////////////////////////////////////////////////////////////////////////
    219.  
    220.  
    221. int checkwin()
    222. {
    223.     if (square[1] == square[2] && square[2] == square[3])return 1;
    224.     if (square[4] == square[5] && square[5] == square[6])return 1;
    225.     if (square[7] == square[8] && square[8] == square[9])return 1;
    226.     if (square[1] == square[4] && square[4] == square[7])return 1;
    227.     if (square[2] == square[5] && square[5] == square[8])return 1;
    228.     if (square[3] == square[6] && square[6] == square[9])return 1;
    229.     if (square[1] == square[5] && square[5] == square[9])return 1;
    230.     if (square[3] == square[5] && square[5] == square[7])return 1;
    231.  
    232. for(int i = 1;i <= 9;i++)if(IsEmpty(i) == true)return -1;
    233.  
    234.     board();
    235.     cout<<"==>\aGame draw\n";
    236.     system("pause");
    237.     exit(0);
    238.     return 0;
    239. }
    240.  
    241.  
    242. void board()
    243. {
    244. system("cls");
    245. cout << "\n\n\tTic Tac Toe\n\n";
    246. cout << "Player 1 (X) - Computer (O)" << endl << endl << endl;
    247. cout << "____________" << endl;
    248. cout << "|  |   |   |  " << endl;
    249. cout << "|" << square[1] << " | " << square[2] << " | " << square[3] << " | " << endl;
    250. cout << "____________" << endl;
    251. cout << "|  |   |   |  " << endl;
    252. cout << "|" << square[4] << " | " << square[5] << " | " << square[6] <<  " | " << endl;
    253. cout << "____________" << endl;
    254. cout << "|  |   |   |  " << endl;
    255. cout << "|" << square[7] << " | " << square[8] << " | " << square[9] <<  " | " << endl;
    256. cout << "____________" << endl;
    257. }

    Test it, and let me know what you think.
    Any suggestion?

  3. #3
    Ngày gia nhập
    07 2006
    Nơi ở
    Hanoi, Vietnam
    Bài viết
    2,750

    The first look: The source code was written without conventional, but nice...
    Email: admin[@]congdongcviet.com | CC to: info[@]congdongcviet.com
    Phone: 0972 89 7667 (Office: 04 6329 2380)
    Yahoo & Skype: dreaminess_world (Vui lòng chỉ rõ mục đích ngay khi liên hệ, cảm ơn!)

    Một người nào đó coi thường ý thức kỷ luật cũng có nghĩa là người đó đã coi thường tương lai số phận của chính bản thân người đó. Những người coi thường ý thức kỷ luật sẽ không bao giờ có được sự thành công trong sự nghiệp!

  4. #4
    Ngày gia nhập
    12 2012
    Bài viết
    7

    The first look: The source code was written without conventional.
    Wrong grammar.
    The first look: The source code was written without conventional rule.
    without is never followed by "adjective"
    I don't really understand your comment. It's very general.

    It seems my code is very complex, let's understand :

    1.
    C++ Code:
    1. #define X 1
    2. #define Y 0
    3.  
    4. int main()
    5. {
    6.     srand(time(NULL));
    7.     bool player = X;
    8.     char mark;
    9.     int i, choice;
    10.     cout << "Computer first? (Press Y) : ";
    11.     cin >> mark;
    12.     if(mark == 'y' || mark == 'Y')player = Y;
    13.     system("cls");
    14. ..........................................................

    I used #define definition.
    So I can write :
    C++ Code:
    1. if(player == X){[...]} //If current player is X
    2. if(player == Y){[...]} //If current player is Y

    Next, the code :
    C++ Code:
    1.  srand(time(NULL));
    Re-initiates the random generator.

    2.

    C++ Code:
    1. char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
    2.  
    3. void board()
    4. {
    5. system("cls");
    6. cout << "\n\n\tTic Tac Toe\n\n";
    7. cout << "Player 1 (X) - Computer (O)" << endl << endl << endl;
    8. cout << "____________" << endl;
    9. cout << "|  |   |   |  " << endl;
    10. cout << "|" << square[1] << " | " << square[2] << " | " << square[3] << " | " << endl;
    11. cout << "____________" << endl;
    12. cout << "|  |   |   |  " << endl;
    13. cout << "|" << square[4] << " | " << square[5] << " | " << square[6] <<  " | " << endl;
    14. cout << "____________" << endl;
    15. cout << "|  |   |   |  " << endl;
    16. cout << "|" << square[7] << " | " << square[8] << " | " << square[9] <<  " | " << endl;
    17. cout << "____________" << endl;
    18. }
    Here's how to print a 9-element board.

    Next, how to fill out the Tick Tack Toe board with "X" and "O"?
    The first player (X) always is a human.
    The second player (O) always is a fighter (CPU).

    Then, how to set a board element :
    C++ Code:
    1. square[nIndex] = 'X'; (Range : 1 - 9)
    2. square[nIndex] = 'O';

    And a function which is used to handle the user choice.
    C++ Code:
    1. bool SetItem(int nNumber, int mark){
    2. if(square[nNumber] != nNumber + '0' || nNumber < 1 || nNumber > 9)return false;
    3. square[nNumber] = mark;return true;}

    Why? Note the default definition :
    C++ Code:
    1. char square[10] = {'o','1','2','3','4','5','6','7','8','9'}; //'o' : is unused

    3. Here is the machine which manages the human input.
    C++ Code:
    1.    mark = (player == X) ? 'X' : 'O'; //Very important !! Because the board is a char-array.
    2.     if(player == X)
    3.     {
    4.         cout << "Player " << player << " ("<< mark <<"), please enter a number : ";
    5.         cin >> choice; //Input the user choice
    6.         if(SetItem(choice, mark) == false){ //Fail?
    7.     cout<<"Invalid move ";
    8.     player = !player; //player = (player == X) ? 'O' : 'X';
    9.     cin.ignore();
    10.     cin.get();
    11. }
    12. }
    13. else Think(player); //Hahaha....

    And, how to check a valid column, or row, or diagonally line of "X" - "O"?

    To make this, I used "IsEmpty()
    C++ Code:
    1. bool IsEmpty(int nIndex){return (square[nIndex] == '0' + nIndex);}
    2.  
    3. //bool IsEmpty(int nIndex){if(square[nIndex] == '0' + nIndex)return true; else return false;}

    C++ Code:
    1. int checkwin() //This requires some of code logic :)
    2. {
    3.     if (square[1] == square[2] && square[2] == square[3])return 1;
    4.     if (square[4] == square[5] && square[5] == square[6])return 1;
    5.     if (square[7] == square[8] && square[8] == square[9])return 1;
    6.     if (square[1] == square[4] && square[4] == square[7])return 1;
    7.     if (square[2] == square[5] && square[5] == square[8])return 1;
    8.     if (square[3] == square[6] && square[6] == square[9])return 1;
    9.     if (square[1] == square[5] && square[5] == square[9])return 1;
    10.     if (square[3] == square[5] && square[5] == square[7])return 1;
    11.  
    12. for(int i = 1;i <= 9;i++)if(IsEmpty(i) == true)return -1; //If the board is not full
    13. /////////////////////////////////////////////////////////////////////
    14.     board();
    15.     cout<<"==>\aGame draw\n";
    16.     system("pause");
    17.     exit(0);
    18.     return 0;
    19. }

    Continue... Try thinking how can the computer code vs human?
    Đã được chỉnh sửa lần cuối bởi codefighter : 19-12-2012 lúc 07:46 PM.

Các đề tài tương tự

  1. Bài tập C++ Simple c++ html sever
    Gửi bởi Kal trong diễn đàn Nhập môn lập trình C/C++
    Trả lời: 0
    Bài viết cuối: 02-04-2012, 04:29 PM
  2. Sự kiện tick của timer trong project của em !
    Gửi bởi joe_ddo trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 14
    Bài viết cuối: 07-01-2011, 08:55 PM
  3. Mã nguồn C Lỗi Code simple caculator
    Gửi bởi Super_chicken trong diễn đàn Thắc mắc lập trình C/C++/C++0x
    Trả lời: 4
    Bài viết cuối: 22-11-2010, 02:15 AM
  4. Tick vào ô textbox thì select all
    Gửi bởi antuong12c2 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 3
    Bài viết cuối: 17-10-2009, 08:29 AM
  5. [BT]Simple calculations
    Gửi bởi hailoc12 trong diễn đàn Thắc mắc lập trình C/C++/C++0x
    Trả lời: 4
    Bài viết cuối: 25-06-2007, 07:10 PM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn