Code: Show/Hide /* Tic-Tac-Toe
** By Kevin J. Russell ** 2010.01.19 January 19th, 2010 ** ** This is my first game. ** It's Tic-Tac-Toe in C++/SDL. */ /* Includes */ #include "SDL/SDL.h" #include "SDL/SDL_image.h" /* Screen Attributes */ const int SCREEN_WIDTH = 600; const int SCREEN_HEIGHT = 600; const int SCREEN_BPP = 32; enum Mark { markOpen, markX, markO }; /* Function Prototypes */ void ApplySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination); void MarkSquare(int square, Mark mark); void Logic(); /* Surfaces */ SDL_Surface *screen = NULL; SDL_Surface *background = NULL; SDL_Surface *x = NULL; SDL_Surface *o = NULL; SDL_Surface *win = NULL; SDL_Event event; Mark board[10]; Mark mark = markO; bool quit = false; int main(int argc, char *args[]) { for (int i = 0; i < 10; i++) board[i] = markOpen; SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); SDL_WM_SetCaption("Tic-Tac-Toe", NULL); background = SDL_DisplayFormat(IMG_Load("background.png")); x = SDL_DisplayFormat(IMG_Load("x.png")); o = SDL_DisplayFormat(IMG_Load("o.png")); ApplySurface(0, 0, background, screen); SDL_Flip(screen); while (quit == false) { while (SDL_PollEvent(&event)) { if (event.type == SDL_MOUSEBUTTONDOWN) { // Sneaky algorithm to convert mouse pos to square number MarkSquare((event.button.x/200) + (event.button.y/200)*3 +1, mark); Logic(); } if (event.type == SDL_QUIT) quit = true; } } SDL_FreeSurface(win); SDL_FreeSurface(background); SDL_FreeSurface(x); SDL_FreeSurface(o); SDL_Quit(); return 0; } void ApplySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination) { SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(source, NULL, destination, &offset); } void MarkSquare(int square, Mark mark) { board[square] = mark; // Get placement co-ords based on square number. int xpos = (((square-1)%3) *200) +10; int ypos = (((square-1)/3) *200) +10; if (mark == markX) ApplySurface(xpos, ypos, x, screen); else if (mark == markO) ApplySurface(xpos, ypos, o, screen); } void Logic() { if (mark == markX) mark = markO; else if (mark == markO) mark = markX; // Victory Check if (board[1] == board[2] && board[1] == board[3] && board[1] != markOpen) { if (board[1] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } else if (board[4] == board[5] && board[4] == board[6] && board[4] != markOpen) { if (board[4] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } else if (board[7] == board[8] && board[7] == board[9] && board[7] != markOpen) { if (board[7] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } else if (board[1] == board[4] && board[1] == board[7] && board[1] != markOpen) { if (board[1] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } else if (board[2] == board[5] && board[2] == board[8] && board[2] != markOpen) { if (board[2] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } else if (board[3] == board[6] && board[3] == board[9] && board[3] != markOpen) { if (board[3] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } else if (board[1] == board[5] && board[1] == board[9] && board[1] != markOpen) { if (board[1] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } else if (board[3] == board[5] && board[3] == board[7] && board[3] != markOpen) { if (board[3] == markX) win = SDL_DisplayFormat(IMG_Load("xwin.png")); else win = SDL_DisplayFormat(IMG_Load("owin.png")); ApplySurface(0, 201, win, screen); } SDL_Flip(screen); } |
Code: Show/Hide if (event.type == SDL_MOUSEBUTTONDOWN)
{ if (event.button.x <= 200 && event.button.y <= 200) MarkSquare(1, mark); if (event.button.x > 200 && event.button.x <= 400 && event.button.y <= 200) MarkSquare(2, mark); if (event.button.x > 400 && event.button.y <= 200) MarkSquare(3, mark); if (event.button.x <= 200 && event.button.y > 200 && event.button.y <= 400) MarkSquare(4, mark); if (event.button.x > 200 && event.button.x <= 400 && event.button.y > 200 && event.button.y <= 400) MarkSquare(5, mark); if (event.button.x > 400 && event.button.y > 200 && event.button.y <= 400) MarkSquare(6, mark); if (event.button.x <= 200 && event.button.y > 400) MarkSquare(7, mark); if (event.button.x > 200 && event.button.x <= 400 && event.button.y > 400) MarkSquare(8, mark); if (event.button.x > 400 && event.button.y > 400) MarkSquare(9, mark); Logic(); } |
Code: Show/Hide //check rows for (int j = 0; j < 3; j++) //for each row if (board[0][j] != markOpen && board[0][j] == board[1][j] && board[0][j] == board[2][j]) win(board[0][j]); //where win is a function that takes a Mark argument //do the same for columns |
Code: Show/Hide /* Tic-Tac-Toe
** By Kevin J. Russell ** 2010.01.19 January 19th, 2010 ** ** This is my first game. ** It's Tic-Tac-Toe in C++/SDL. */ /* Includes */ #include "SDL/SDL.h" #include "SDL/SDL_image.h" /* Screen Attributes */ const int SCREEN_WIDTH = 600; const int SCREEN_HEIGHT = 600; const int SCREEN_BPP = 32; enum Mark { markOpen, markX, markO }; /* Function Prototypes */ void ApplySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination); void MarkSquare(int squarex, int squarey, Mark mark); void Logic(); /* Surfaces */ SDL_Surface *screen = NULL; SDL_Surface *background = NULL; SDL_Surface *x = NULL; SDL_Surface *o = NULL; SDL_Surface *win = NULL; SDL_Event event; //Mark board[10]; Mark board[3][3]; Mark mark = markO; bool quit = false; int main(int argc, char *args[]) { /*for (int i = 0; i < 10; i++) board[i] = markOpen;*/ for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) board[i][j] = markOpen; SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); SDL_WM_SetCaption("Tic-Tac-Toe", NULL); background = SDL_DisplayFormat(IMG_Load("background.png")); x = SDL_DisplayFormat(IMG_Load("x.png")); o = SDL_DisplayFormat(IMG_Load("o.png")); ApplySurface(0, 0, background, screen); SDL_Flip(screen); while (quit == false) { while (SDL_PollEvent(&event)) { if (event.type == SDL_MOUSEBUTTONDOWN) { // Sneaky algorithm to convert mouse pos to square co-ord MarkSquare(event.button.x/200, event.button.y/200, mark); Logic(); } if (event.type == SDL_QUIT) quit = true; } } SDL_FreeSurface(win); SDL_FreeSurface(background); SDL_FreeSurface(x); SDL_FreeSurface(o); SDL_Quit(); return 0; } void ApplySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination) { SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(source, NULL, destination, &offset); } void MarkSquare(int squarex, int squarey, Mark mark) { board[squarex][squarey] = mark; // Get placement co-ords based on square number if (mark == markX) ApplySurface(200 * squarex + 10, 200 * squarey + 10, x, screen); else if (mark == markO) ApplySurface(200 * squarex + 10, 200 * squarey + 10, o, screen); } void Logic() { // Set the win surface to the previous player's mark, in case of victory. // Then flip the mark for the next turn. if (mark == markX) { win = SDL_DisplayFormat(IMG_Load("xwin.png")); mark = markO; } else { win = SDL_DisplayFormat(IMG_Load("owin.png")); mark = markX; } // Victory Check for (int i = 0; i < 3; i++) { if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != markOpen) ApplySurface(0, 201, win, screen); if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != markOpen) ApplySurface(0, 201, win, screen); } if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != markOpen) ApplySurface(0, 201, win, screen); if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != markOpen) ApplySurface(0, 201, win, screen); SDL_Flip(screen); } |
Code: Show/Hide Label start; |
Code: Show/Hide goTo(start); |
Samapico wrote: |
Don't listen to rootbear, he wants to make you program like it's 1985 again.
You can make your main loop back at the beginning in a much cleaner way You might only need to dump the initialization stuff in a function, and call that function when you hit restart. |
Animate Dreams wrote: |
Ah, there is so little initialization, I think it just clutters things to move it into a function. |