Advertisement
Guest User

Tic-Tac-Toe

a guest
Jan 10th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 6.22 KB | None | 0 0
  1. import std.stdio; //For console I/O
  2. import std.random; //We need this for the randomness of the CPU move
  3. import std.process; //For clearing the console screen - system("cls")
  4.  
  5. char[3][3] board = [['1','2','3'],['4','5','6'],['7','8','9']]; //The X/O board
  6.  
  7. struct Player //The player structure. Contains player's mark and win data.
  8. {
  9.     char[1] mark;
  10.     bool winner;
  11.     int inp;
  12. }
  13.  
  14. void printBoard() //Function to print the board
  15. {
  16.     writeln(" ",board[0][0]," | ",board[0][1]," | ",board[0][2]);
  17.     writeln("---","|","---","|","---");
  18.     writeln(" ",board[1][0]," | ",board[1][1]," | ",board[1][2]);
  19.     writeln("---","|","---","|","---");
  20.     writeln(" ",board[2][0]," | ",board[2][1]," | ",board[2][2]);
  21. }
  22.  
  23. bool checkWin(Player who) //Function to check the winner
  24. {
  25.     if(board[0][0] == who.mark[0] && board[1][1] == who.mark[0] && board[2][2] == who.mark[0])
  26.     {
  27.         return true;    
  28.     }
  29.     else if(board[0][2] == who.mark[0] && board[1][1] == who.mark[0] && board[2][0] == who.mark[0])
  30.     {
  31.         return true;
  32.     }
  33.     else if(board[0][0] == who.mark[0] && board[0][1] == who.mark[0] && board[0][2] == who.mark[0])
  34.     {
  35.         return true;
  36.     }
  37.     else if(board[1][0] == who.mark[0] && board[1][1] == who.mark[0] && board[1][2] == who.mark[0])
  38.     {
  39.         return true;
  40.     }
  41.     else if(board[2][0] == who.mark[0] && board[2][1] == who.mark[0] && board[2][2] == who.mark[0])
  42.     {
  43.         return true;
  44.     }
  45.     else if(board[0][0] == who.mark[0] && board[1][0] == who.mark[0] && board[2][0] == who.mark[0])
  46.     {
  47.         return true;
  48.     }
  49.     else if(board[0][1] == who.mark[0] && board[1][1] == who.mark[0] && board[2][1] == who.mark[0])
  50.     {
  51.         return true;
  52.     }
  53.     else if(board[0][2] == who.mark[0] && board[1][2] == who.mark[0] && board[2][2] == who.mark[0])
  54.     {
  55.         return true;
  56.     }
  57.     else
  58.     {
  59.         return false;
  60.     }
  61. }
  62.  
  63. int setMark(Player who) //Function to set the mark on the board
  64. {
  65.     int input = who.inp-1;
  66.    
  67.     switch(input)
  68.     {
  69.         case 0:
  70.             if(board[0][0] == 'X' || board[0][0] == 'O')
  71.             {
  72.                 return 1;
  73.             }
  74.             board[0][0] = who.mark[0];
  75.             break;
  76.            
  77.         case 1:
  78.             if(board[0][1] == 'X' || board[0][1] == 'O')
  79.             {
  80.                 return 1;
  81.             }
  82.             board[0][1] = who.mark[0];
  83.             break;
  84.            
  85.         case 2:
  86.             if(board[0][2] == 'X' || board[0][2] == 'O')
  87.             {
  88.                 return 1;
  89.             }
  90.             board[0][2] = who.mark[0];
  91.             break;
  92.            
  93.         case 3:
  94.             if(board[1][0] == 'X' || board[1][0] == 'O')
  95.             {
  96.                 return 1;
  97.             }
  98.             board[1][0] = who.mark[0];
  99.             break;
  100.            
  101.         case 4:
  102.             if(board[1][1] == 'X' || board[1][1] == 'O')
  103.             {
  104.                 return 1;
  105.             }
  106.             board[1][1] = who.mark[0];
  107.             break;
  108.            
  109.         case 5:
  110.             if(board[1][2] == 'X' || board[1][2] == 'O')
  111.             {
  112.                 return 1;
  113.             }
  114.             board[1][2] = who.mark[0];
  115.             break;
  116.            
  117.         case 6:
  118.             if(board[2][0] == 'X' || board[2][0] == 'O')
  119.             {
  120.                 return 1;
  121.             }
  122.             board[2][0] = who.mark[0];
  123.             break;
  124.            
  125.         case 7:
  126.             if(board[2][1] == 'X' || board[2][1] == 'O')
  127.             {
  128.                 return 1;
  129.             }
  130.             board[2][1] = who.mark[0];
  131.             break;
  132.            
  133.         case 8:
  134.             if(board[2][2] == 'X' || board[2][2] == 'O')
  135.             {
  136.                 return 1;
  137.             }
  138.             board[2][2] = who.mark[0];
  139.             break;
  140.            
  141.         default:
  142.             return 1;
  143.             break;
  144.     }
  145.     return 0;
  146. }
  147.  
  148. void declareWinner(Player p1, Player p2) //Function to declare winner
  149. {
  150.     if(p1.winner)
  151.     {
  152.         writeln("\n***CONGRATS TO YOU FOR WINNING!***\n");
  153.     }
  154.     else if(p2.winner)
  155.     {
  156.         writeln("\n***CONGRATS TO CPU/P2 FOR WINNING!***\n");
  157.     }
  158. }
  159.  
  160. int main() //The main function
  161. {
  162.     Player player1; //Set up player 1 variables
  163.     player1.mark[0] = 'X';
  164.     player1.winner = false;
  165.     player1.inp = 12;
  166.    
  167.     Player cpu; //Set  up player 2 (cpu) variables
  168.     cpu.mark[0] = 'O';
  169.     cpu.winner = false;
  170.    
  171.    
  172.     while(true) //Main loop
  173.     {
  174.         system("cls");
  175.         writeln("Copyright(c) 2014 - Ahnaf Tahmid\n");
  176.         writefln("Player 1 (You) = %s, CPU = %s\n",player1.mark[0],cpu.mark[0]);
  177.         printBoard();
  178.        
  179.         while(setMark(player1) == 1)
  180.         {
  181.             write("\nEnter a position: ");
  182.             readf(" %s", &player1.inp);
  183.             if(setMark(player1) == 1)
  184.             {
  185.                 writeln("\nInvalid position!");
  186.             }
  187.             else
  188.             {
  189.                 break;
  190.             }
  191.         }
  192.        
  193.         player1.winner = checkWin(player1);
  194.        
  195.         if(player1.winner)
  196.         {
  197.             system("cls");
  198.             writeln("Copyright(c) 2014 - Ahnaf Tahmid\n");
  199.             writeln("Player 1 (You) = X, CPU = O\n");
  200.             printBoard();
  201.             declareWinner(player1, cpu);
  202.             break;
  203.         }
  204.        
  205.         cpu.inp = uniform(0,9)+1;
  206.         int errors = 0; //Draw indicator
  207.         while(setMark(cpu) == 1 && errors < 20)
  208.         {
  209.             cpu.inp = uniform(0,9)+1;
  210.             errors++;
  211.         }
  212.        
  213.         cpu.winner = checkWin(cpu);
  214.        
  215.         if(cpu.winner)
  216.         {
  217.             system("cls");
  218.             writeln("Copyright(c) 2014 - Ahnaf Tahmid\n");
  219.             writeln("Player 1 (You) = X, CPU = O\n");
  220.             printBoard();
  221.             declareWinner(player1, cpu);
  222.             break;
  223.         }
  224.        
  225.         if(errors == 20) //This is actually the real draw handling (not the best one around)
  226.         {
  227.             writeln("\n***ITS A DRAW!***\n");
  228.             break;
  229.         }
  230.     }
  231.    
  232.     writeln("Enter any number to exit.");
  233.    
  234.     int dummy_var;
  235.     readf(" %s", &dummy_var);
  236.    
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement