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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
//
// connect4.c
//
//
// Created by Tony Snow on 4/12/13.
//
//
#include <stdio.h>
#include "SDL/SDL.h" // import SimpleDirect media Layer library
/*
Tic-tac-toe
To compile:
gcc tic-tac-toe.c -o connect4.exe -lmingw32 -lSDLmain -lSDL
Or:
gcc tictactoe.c -o tictactoe.exe $(sdl-config --cflags --libs)
*/
// Global variable
SDL_Surface *screen = NULL;
void init() {
// Start SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Quit SDL when it's time
atexit(SDL_Quit);
// Set the caption
SDL_WM_SetCaption("Connect 4!", NULL);
// Set up the screen
screen = SDL_SetVideoMode(98*7, 98*7, 32, SDL_SWSURFACE);
}
typedef enum { O, X, _ } Cell;
/* Show a single cell. */
int lookup (char c) {
if (c == 'O') return 0;
else if (c == 'X') return 1;
else return 2;
}
/* Show the game board using a loop within a loop. */
void show (Cell board[6][7]) {
int i, j;
SDL_Surface *cell[] = {
SDL_LoadBMP("redsquare.bmp"), // Load the O
SDL_LoadBMP("yellowsquare.bmp"), // Load the X
SDL_LoadBMP("emptysquare.bmp") };
SDL_Rect location;
for (i = 0; i < 7; i++) {
for (j = 0; j < 6; j++) {
location.x = i * 98;
location.y = j * 98;
SDL_BlitSurface (cell[board[i][j]], NULL,
screen, &location);
}
}
SDL_Flip(screen); // updates the screen
SDL_FreeSurface(cell[0]);
SDL_FreeSurface(cell[1]);
SDL_FreeSurface(cell[2]);
SDL_FreeSurface(cell[3]);
SDL_FreeSurface(cell[4]);
SDL_FreeSurface(cell[5]);
}
/* Return whether number is between 0 and 6 */
int valid (int number) {
return number >= 0 && number < 6;
}
/* Ask the player to make a move. */
void play_turn (Cell board[6][7], Cell player) {
int row, col;
SDL_Event event;
SDL_Rect location;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:// if event.type = mousebuttondown
row = event.button.x / 160;
col = event.button.y / 160;
if (valid(row) && valid(col)
&& board [row] [col] == _) {
board[row][col] = player;
return;
}
else {
break;
case SDL_QUIT:
exit(0);
return;
}
}
board[row][col] = player;
}
}
/*
Return the new player, given the old player
When we talk about pointers, this would be a void function with Cell *oldPlayer parameter
*/
Cell switch_player (Cell oldPlayer) {
// Feel free to also show the ternary expression thing
// return (player == O) ? X : O;
if (oldPlayer == O) {
return X;
} else {
return O;
}
}
/* Initialize the game board and describe tic-tac-toe at a high level */
int main (int argc, char ** argv) {
Cell board[6][7] = { { _, _, _, _, _, _, _}, { _, _, _, _, _, _, _}, { _, _, _, _, _, _, _}, { _, _, _, _, _, _, _},{ _, _, _, _, _, _, _}, { _, _, _, _, _, _, _} };
Cell player = O;
init(); // Call that SDL initialization stuff
/* Decide when the game is over */
int game_over (Cell board[6][7]) {
int i, j;
for (i=0; i<6; i++)
{
for(j=0; j<7; j++)
{
if(Cell board[i][j]==O&&
Cell board[i][j+1]==O
&&Cell board[i][j+2]==O
&&Cell board[i][j+3]==O||
Cell board[i][j]==O
&&Cell board[i+1][j]==O
&&Cell board[i+2][j]==O
&&Cell board[i+3][j]==O||
Cell board[i][j]==O
&&Cell board[i+1][j+1]==O
&&Cell board[i+2][j+2]==O
&&Cell board[i+3][j+3]==O||
Cell board[i][j]==O
&&Cell board[i+1][j-1]==O
&&Cell board[i+2][j-2]==O
&&Cell board[i+3][j-3]==O)
{
printf("red wins!");
}
else if (
Cell board[i][j]==X
&&Cell board[i][j+1]==X
&&Cell board[i][j+2]==X
&&Cell board[i][j+3]==X||
Cell board[i][j]==X
&&Cell board[i+1][j]==X
&&Cell board[i+2][j]==X
&&Cell board[i+3][j]==X||
Cell board[i][j]==X
&&Cell board[i+1][j+1]==X
&&Cell board[i+2][j+2]==X
&&Cell board[i+3][j+3]==X||
Cell board[i][j]==X
&&Cell board[i+1][j-1]==X
&&Cell board[i+2][j-2]==X
&&Cell board[i+3][j-3]==X)
{
printf("yellow wins!");
}
}
}
return 0; // Exercise for the students :-)
}
while (!game_over (board)) {
show (board);
play_turn (board, player);
player = switch_player (player);
}
}
|