Advertisement
Guest User

Tic, tac, toe command line

a guest
Jan 11th, 2012
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. /////You gonna get raped
  2. #include <iostream>
  3. //#include <cmath>
  4. //#include <vector>
  5. //#include <cstdlib>
  6. //#include <iomanip>
  7. //#include "std_lib_facilities.h"
  8.  
  9. using namespace std;
  10.  
  11. //////Tic, tac, toe for command line//////
  12.  
  13. //      This program is free software; you can redistribute it and/or modify
  14. //      it under the terms of the GNU General Public License as published by
  15. //      the Free Software Foundation; either version 2 of the License, or
  16. //      (at your option) any later version.
  17. //      
  18. //      This program is distributed in the hope that it will be useful,
  19. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. //      GNU General Public License for more details.
  22. //      
  23. //      You should have received a copy of the GNU General Public License
  24. //      along with this program; if not, write to the Free Software
  25. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  26. //      MA 02110-1301, USA.
  27. ///////////////////////////////// >inb4 nobody wants your piece of shit code
  28.  
  29. //class wrong_x_or_o { };
  30. class false_win { };
  31. class already_used { };
  32. class bad_pos_input { };
  33.  
  34. string pos1 = "1";
  35. string pos2 = "2";
  36. string pos3 = "3";
  37. string pos4 = "4";
  38. string pos5 = "5";
  39. string pos6 = "6";
  40. string pos7 = "7";
  41. string pos8 = "8";
  42. string pos9 = "9";
  43. bool pos1_changed = false;
  44. bool pos2_changed = false;
  45. bool pos3_changed = false;
  46. bool pos4_changed = false;
  47. bool pos5_changed = false;
  48. bool pos6_changed = false;
  49. bool pos7_changed = false;
  50. bool pos8_changed = false;
  51. bool pos9_changed = false;
  52. bool win=false;
  53. string winner="nobody";
  54. int checks = 0;
  55. bool tie = false;
  56.  
  57. void change_it(int pos_specify, string x_or_o) {
  58. //  if(x_or_o != "x" && x_or_o !="X" && x_or_o != "o" && x_or_o != "O") {
  59. //      throw wrong_x_or_o(); }
  60. //  if(x_or_o == "x") x_or_o="X";
  61. //  if(x_or_o == "o") x_or_o="O";
  62.     if(!cin) throw bad_pos_input(); //check if anything but int
  63.     if(pos_specify>9 || pos_specify<1) throw bad_pos_input();
  64.     if(pos_specify==1) {
  65.         if(pos1_changed==true) throw already_used();
  66.         pos1=x_or_o;
  67.         pos1_changed=true;
  68.     }
  69.        
  70.     if(pos_specify==2) {
  71.         if(pos2_changed==true) throw already_used();
  72.         pos2=x_or_o;
  73.         pos2_changed=true;
  74.     }
  75.    
  76.     if(pos_specify==3) {
  77.         if(pos3_changed==true) throw already_used();
  78.         pos3=x_or_o;
  79.         pos3_changed=true;
  80.     }
  81.    
  82.     if(pos_specify==4) {
  83.         if(pos4_changed==true) throw already_used();
  84.         pos4=x_or_o;
  85.         pos4_changed=true;
  86.     }
  87.    
  88.     if(pos_specify==5) {
  89.         if(pos5_changed==true) throw already_used();
  90.         pos5=x_or_o;
  91.         pos5_changed=true;
  92.     }
  93.  
  94.     if(pos_specify==6) {
  95.         if(pos6_changed==true) throw already_used();
  96.         pos6=x_or_o;
  97.         pos6_changed=true;
  98.     }
  99.  
  100.     if(pos_specify==7) {
  101.         if(pos7_changed==true) throw already_used();
  102.         pos7=x_or_o;
  103.         pos7_changed=true;
  104.     }
  105.  
  106.     if(pos_specify==8) {
  107.         if(pos8_changed==true) throw already_used();
  108.         pos8=x_or_o;
  109.         pos8_changed=true;
  110.     }
  111.  
  112.     if(pos_specify==9) {
  113.         if(pos9_changed==true) throw already_used();
  114.         pos9=x_or_o;
  115.         pos9_changed=true;
  116.     }
  117. }
  118.  
  119. void draw_board() {
  120.     cout << pos1 << "\t" << pos2 << "\t" << pos3 << "\n" << pos4
  121.     << "\t" << pos5 << "\t" << pos6 << "\n" << pos7 << "\t"
  122.     << pos8 << "\t" << pos9 << endl;
  123. }
  124.  
  125. void win_logic_append() {
  126.     if(winner=="nobody") throw false_win();
  127.     cout << winner << " wins!\n\n";
  128.     win=true;
  129. }
  130.  
  131. int main() {
  132.     //string pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9 = "f";
  133.    
  134.     //draw the board
  135.     draw_board();
  136.    
  137.     //gameplay
  138.     int pos_specify = 0;
  139.     string x_or_o = " ";
  140.     int count = 1;
  141.     string turn = " ";
  142.    
  143.     cout << "\nX plays first.\n";
  144.  
  145.     while(win==false) {
  146.         //////////determine turn
  147.         if( (count%2)==0 ) { //if even
  148.             //change_it(pos_specify, "O");
  149.             turn="O";
  150.             ++count;
  151.         }
  152.         else { //if odd
  153.             //change_it(pos_specify, "X");
  154.             turn="X";
  155.             ++count;
  156.         }
  157.         //end of determine turn
  158.        
  159.         cout << "\n\n" << turn << "'s turn. What position? (1-9)\n";
  160.         cin >> pos_specify;
  161.         //cout << "What would you like to change it to? (X or O)\n";
  162.         try {
  163.             cout << "\n\n";
  164.             change_it(pos_specify, turn);
  165.             draw_board();
  166.         }
  167.         /*catch (wrong_x_or_o) {
  168.             cerr << "Wrong input for X or O. It may only be X or O. Try again.\n";
  169.         }*/
  170.         catch (already_used) {
  171.             cout << "That square is occupied. Try a different one.\n";
  172.         }
  173.         catch (bad_pos_input) {
  174.             win=true; //not really, just want to terminate loop
  175.             //tie=false;
  176.             cout << "Bad input for your position. It can only be digits 1-9.\n";
  177.         }
  178.         try {
  179.             //logic
  180.             if(pos1==pos2 && pos2==pos3) {
  181.                 winner=pos1;
  182.                 win_logic_append(); }
  183.             else if(count==9) ++checks;
  184.             if(pos4==pos5 && pos5==pos6) {
  185.                 winner=pos4;
  186.                 win_logic_append(); }
  187.             else if(count==9) ++checks;
  188.             if(pos7==pos8 && pos8==pos9) {
  189.                 winner=pos7;
  190.                 win_logic_append(); }
  191.             else if(count==9) ++checks;
  192.             if(pos1==pos4 && pos4==pos7) {
  193.                 winner=pos1;
  194.                 win_logic_append(); }
  195.             else if(count==9) ++checks;
  196.             if(pos2==pos5 && pos5==pos8) {
  197.                 winner=pos2;
  198.                 win_logic_append(); }
  199.             else if(count==9) ++checks;
  200.             if(pos3==pos6 && pos6==pos9) {
  201.                 winner=pos3;
  202.                 win_logic_append(); }
  203.             else if(count==9) ++checks;
  204.             if(pos1==pos5 && pos5==pos9) {
  205.                 winner=pos1;
  206.                 win_logic_append(); }
  207.             else if(count==9) ++checks;
  208.             if(pos3==pos5 && pos5==pos7) {
  209.                 winner=pos3;
  210.                 win_logic_append(); }
  211.             else if(count==9) ++checks;
  212.             if(checks==8) {
  213.                 tie=true;
  214.                 win=true;
  215.                 cout << "\nTie game!\n";
  216.             }
  217.         }
  218.         catch (false_win) {cerr << "Error: False win.\n";}
  219.     }
  220.    
  221.     return 0;
  222. }
  223. //////I dun told you was gonna get raped
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement