-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDP_Connect4.m
More file actions
135 lines (105 loc) · 3.78 KB
/
Copy pathSDP_Connect4.m
File metadata and controls
135 lines (105 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
%% Software Design Project - Connect 4
% Group L
% workspace setup
clc; clear;
%% SimpleGameEngine setup
% Initialize simpleGameEngine object with spritesheet
scene = simpleGameEngine('SpritesheetOSUvMichigan.png',16,16,4,[250,250,250]);
% List of sprite sheet contents, can be displayed by using drawScene
sprite_sheet_contents = [[1 2 3 4];[5 6 7 8];[9 10 11 12];[13 14 15 16]];
%% Variables
% A blank board layout
blankBoard = [
4 5 6 7 8 9 10;
ones(6, 7)
];
% Player variables
% Player 1: chip sprite index, marker sprite index, win count
% Player 2: chip sprite index, marker sprite index, win count
players = [
2, 11, 0;
3, 12, 0;
];
% Turn variable
turn = 0;
% GameOver flag
gameOver = false;
%% Play the Game
% Outer game loop - allows the user to play again and reset the board state
while true
% Reset the board and draw it
currentBoard = blankBoard;
drawScene(scene, currentBoard);
% Reset the turn counter
turn = 0;
% Reset the gameOver flag
gameOver = false;
% Start marker in middle of board (displaying the marker later adds 1)
markerIndex = 3;
% Array with the top row of numbers
numRow = blankBoard(1,:);
% Win count (scoreboard) along the bottom
xlabel(sprintf("OSU: %d | Michigan: %d",players(1,3),players(2,3)))
% Innner game loop - plays a game of connect 4
while ~gameOver
title(sprintf("Turn %d",turn+1))
% Game must end at turn 42 in a draw
if turn == 42
title("Draw")
gameOver = true;
break
end
% Check who's turn it is
currentPlayer = mod(turn, 2) + 1; % Corresponds to a row in players
% Put the marker onscreen
currentBoard(1,:) = numRow;
currentBoard(1,mod(markerIndex,7)+1) = ...
players(currentPlayer, 2);
drawScene(scene, currentBoard);
chipPlaced = false;
while ~chipPlaced
key = getKeyboardInput(scene);
if strcmp(key,'leftarrow')
% Move the marker to the left and wrap around at 7 and 0
markerIndex = markerIndex - 1;
elseif strcmp(key,'rightarrow')
% Move the marker to the right and wrap around at 7 and 0
markerIndex = markerIndex + 1;
elseif strcmp(key,'space')
[chipPlaced, currentBoard] = placeChip(currentBoard, ...
players(currentPlayer,:));
end
currentBoard(1,:) = numRow;
currentBoard(1,mod(markerIndex,7)+1) = ...
players(currentPlayer, 2);
drawScene(scene, currentBoard);
end
% Check if the game has been won / board has been filled
gameOver = checkForWin(currentBoard,players(1,1),players(2,1));
if gameOver
% Increment the winning player's win count
players(currentPlayer,3) = players(currentPlayer,3) + 1;
xlabel(sprintf("OSU: %d | Michigan: %d", ...
players(1,3),players(2,3)))
% Fill the top row of the board with the player's marker
currentBoard(1,:) = players(currentPlayer,2);
drawScene(scene, currentBoard);
% Display who won
if currentPlayer == 2
title("❌ichigan Wins")
else
title("OSU Wins")
end
% Break out of the inner loop
break
end
% Increment turn
turn = turn + 1;
end
% Display that the game is over
ylabel("Game Over -- Press Any Key to Play Again")
% Wait for keyboard input to continue
playAgainKey = getKeyboardInput(scene);
% Reset the y axis label
ylabel("")
end