Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bool=isConnect4(board)
- bool = 0
- for y = 1:7
- for x = 1:6
- initial = board(x,y);
- if (x<3) then
- if (board(x+1,y) == initial) & (board(x+2,y) == initial) & (board(x+3,y) == initial))
- bool = 1
- end
- end
- if (y<4) then
- if (board(x,y+1) == initial) & (board(x,y+2) == initial) & (board(x,y+3) == initial)) then
- bool = 1
- end
- end
- if ((y<4) & (x<3)) then
- if (board(x+1,y+1) == initial) & (board(x+2,y+2) == initial) & (board(x+3,y+3) == initial)) then
- bool = 1
- end
- end
- if (bool == 1) then
- break;
- end
- end
- end
- endfunction
- function [row] = determine_row(col, game_board)
- row = 1
- dropping = 1
- while (dropping == 1)
- if (row < 6) & (game_board(row,col) == 0) then
- row = row + 1
- else
- dropping = 0
- end
- end
- if (game_board(row,col) == 1) | (game_board(row,col) == 2) then
- row = row - 1
- end
- endfunction
- exec('isConnect4.sci')
- exec('determine_row.sci')
- //Shows an empty game game_board
- 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]
- disp(game_board)
- player = 1
- running = 1
- rounds = 0
- while (running == 1)
- col = input ('Enter a column to drop your token: ')
- row = determine_row (col, game_board)
- if (row > 0) then
- game_board(row,col) = player
- player = modulo(player,2) + 1
- else
- disp("invalid move")
- end
- disp(game_board)
- //Tie condition
- rounds = rounds + 1
- if rounds > 42 then
- running = 0
- disp('Seriously? It is connect four, how did you get a tie?!')
- end
- //Vicotry Condition
- if (isConnect4(game_board) == 1) then
- disp(player)
- disp("won")
- running = 0;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement