Advertisement
pkramer

Connect4.sce

Jul 24th, 2011
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 2.14 KB | None | 0 0
  1. function bool=isConnect4(board)
  2.  
  3.     bool = 0
  4.  
  5.     for y = 1:7
  6.         for x = 1:6
  7.  
  8.             initial = board(x,y);
  9.            
  10.             if (x<3) then
  11.                 if (board(x+1,y) == initial) & (board(x+2,y) == initial) & (board(x+3,y) == initial))
  12.                     bool = 1
  13.                 end
  14.             end
  15.            
  16.             if (y<4) then
  17.                 if (board(x,y+1) == initial) & (board(x,y+2) == initial) & (board(x,y+3) == initial)) then
  18.                     bool = 1
  19.                 end
  20.             end
  21.            
  22.             if ((y<4) & (x<3)) then
  23.                 if (board(x+1,y+1) == initial) & (board(x+2,y+2) == initial) & (board(x+3,y+3) == initial)) then
  24.                     bool = 1
  25.                 end
  26.             end
  27.            
  28.             if (bool == 1) then
  29.                 break;
  30.             end
  31.         end
  32.     end
  33.  
  34. endfunction
  35.  
  36. function [row] = determine_row(col, game_board)
  37.  
  38.     row = 1
  39.     dropping = 1
  40.    
  41.     while (dropping == 1)
  42.         if (row < 6) & (game_board(row,col) == 0) then
  43.             row = row + 1
  44.         else
  45.             dropping = 0
  46.         end
  47.     end
  48.    
  49.     if (game_board(row,col) == 1) | (game_board(row,col) == 2) then
  50.         row = row - 1
  51.     end
  52.  
  53. endfunction
  54.  
  55. exec('isConnect4.sci')
  56. exec('determine_row.sci')
  57.  
  58. //Shows an empty game game_board
  59. game_board = [0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0; 0 0 0 0 0 0 0]
  60. disp(game_board)
  61.  
  62. player = 1
  63. running = 1
  64. rounds = 0
  65.  
  66. while (running == 1)
  67.  
  68.     col = input ('Enter a column to drop your token: ')
  69.    
  70.     row = determine_row (col, game_board)
  71.    
  72.     if (row > 0) then
  73.         game_board(row,col) = player
  74.         player = modulo(player,2) + 1
  75.     else
  76.         disp("invalid move")
  77.     end
  78.      
  79.     disp(game_board)
  80.    
  81.     //Tie condition
  82.     rounds = rounds + 1
  83.     if rounds > 42 then
  84.         running = 0
  85.         disp('Seriously? It is connect four, how did you get a tie?!')
  86.     end
  87.    
  88.     //Vicotry Condition
  89.     if (isConnect4(game_board) == 1) then
  90.         disp(player)
  91.         disp("won")
  92.         running = 0;
  93.     end
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement