Advertisement
pkramer

tictactoe.m

Jul 18th, 2011
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.33 KB | None | 0 0
  1. board = ['-','-','-';'-','-','-';'-','-','-'];
  2. running = 1;
  3. round = 0;
  4.  
  5. fprintf('Welcome to tictacto bitches!\n');
  6.  
  7. while(running == 1)
  8.  
  9.     %figure whos turn it is
  10.     if(mod(round,2) == 0)
  11.         fprintf('\n~= Xs Turn =~\n');
  12.         turn = 'X';
  13.     else
  14.         fprintf('\n~= Os Turn =~\n');
  15.         turn = 'O';
  16.     end
  17.    
  18.     col = input('Column:');
  19.     row = input('Row:   ');
  20.  
  21.     % check if space if valid, if so then set according to turn
  22.     if (isempty(col) || isempty(row))
  23.         fprintf('\n!> Enter a number dumbass\n');
  24.     else
  25.         if (col >= 0 && col <= 3 && row >= 0 && row <= 3)
  26.             if(board(row,col) == '-')
  27.                 round = round + 1;
  28.                 board(row,col) = turn;
  29.             else
  30.                 fprintf('\n That space is taken jerkoff\n');
  31.             end
  32.         else
  33.             fprintf('\n This is tictactoe idiot, try a space that exists\n');
  34.         end
  35.     end
  36.    
  37.     fprintf('\n');
  38.     disp(board);
  39.     fprintf('\n');    
  40.    
  41.     % VICTORY CONDITIONS
  42.     % row 1
  43.     if(board(1,1) == board(1,2) && board(1,2) == board(1,3))
  44.         if (board(1,1)  ~= '-')
  45.             winner = board(1,1);
  46.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  47.             running = 0;
  48.         end
  49.     end
  50.    
  51.     % row 2
  52.     if(board(2,1) == board(2,2) && board(2,2) == board(2,3))
  53.         if (board(2,1) ~= '-')
  54.             winner = board(2,1);
  55.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  56.             running = 0;
  57.         end
  58.     end
  59.    
  60.     % row 3
  61.     if(board(3,1) == board(3,2) && board(3,2) == board(3,3))
  62.         if(board(3,1)  ~= '-')
  63.             winner = board(3,1);
  64.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  65.             running = 0;
  66.         end
  67.     end
  68.    
  69.     % column 1
  70.     if(board(1,1) == board(2,1) && board(2,1) == board(3,1))
  71.         if (board(1,1)  ~= '-')
  72.             winner = board(1,1);
  73.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  74.             running = 0;
  75.         end
  76.     end
  77.    
  78.     % column 2
  79.     if(board(1,2) == board(2,2) && board(2,2) == board(3,2))
  80.         if (board(1,2)  ~= '-')
  81.             winner = board(1,2);
  82.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  83.             running = 0;
  84.         end
  85.     end
  86.    
  87.     % column 3
  88.     if(board(1,3) == board(2,3) && board(2,3) == board(3,3))
  89.         if (board(1,3)  ~= '-')
  90.             winner = board(1,3);
  91.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  92.             running = 0;
  93.         end
  94.     end
  95.    
  96.     % backward cross
  97.     if(board(1,1) == board(2,2) && board(2,2) == board(3,3))
  98.         if (board(1,1) ~= '-')
  99.             winner = board(1,1);
  100.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  101.             running = 0;
  102.         end
  103.     end
  104.    
  105.     % foward cross
  106.     if(board(3,1) == board(2,2) && board(2,2) == board(1,3))
  107.         if (board(3,1) ~= '-')
  108.             winner = board(3,1);
  109.             fprintf('%s wins... congratualtions on beating yourself without porn\n', winner);
  110.             running = 0;
  111.         end
  112.     end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement