Advertisement
Guest User

100 lines challenge

a guest
Jul 6th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.72 KB | None | 0 0
  1. #include <time.h>
  2. #include <string>
  3. #include <gl\glut.h>
  4. unsigned char gameSpriteO[768] = {0,0,0,0,0,0,255,255,255,255,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,255,255,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,255,255,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,255,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,255,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,255,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,255,255,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,255,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,255,255,255,255,255,255,0,0,0,0,0};
  5. unsigned char gameSpriteX[768] = {255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,255,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,255,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,255,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,255,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,255,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,255,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,255,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,255,0,0,0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,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,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,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  7. int window, mouseButton, mouseClickState, mouseX, mouseY, cpFirstTurn = 1, boardPos = 0, spotIsTaken = 0, whosTurnIsIt = 0, oBlockedYou = 0, timesYouWon = 0;
  8. GLuint gameSpriteOTextureId, gameSpriteXTextureId, gameBoardTextureId;
  9. int xSAndOs[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  10. char formatBuf[512];
  11. void DrawTexture(int textureId, float x, float y, float width, float height) {
  12.     glBindTexture(GL_TEXTURE_2D, textureId);
  13.     glBegin(GL_QUADS);
  14.     glTexCoord2f(0, 0); glVertex3f(x, y + height, 0); glTexCoord2f(1, 0); glVertex3f(x + width, y + height, 0); // bottom left | bottom right
  15.     glTexCoord2f(1, 1); glVertex3f(x + width, y, 0);  glTexCoord2f(0, 1); glVertex3f(x, y, 0); // top right | top left
  16.     glEnd();
  17. }
  18. void MakeTexture(unsigned char* textureData, int textureWidth, int textureHeight, GLuint *outTextureId) {
  19.     glGenTextures(1, outTextureId); glBindTexture(GL_TEXTURE_2D, *outTextureId); // init texture stuff
  20.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, textureData); // generate texture
  21.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // no texture blending
  22. }
  23. void Reset(char *whoWon) {
  24.     sprintf(formatBuf, "%s - %d Times Won\n", whoWon, timesYouWon); glutSetWindowTitle(formatBuf); // Set window message
  25.     Sleep(1000); // wait a moment
  26.     for (int i = 0; i < 9; i++) xSAndOs[i] = 0; whosTurnIsIt = 0; // clear game board
  27. }
  28. void DrawGLScene() {
  29.     glutReshapeWindow(640, 480); // so I dont have to do any more math
  30.     if ((xSAndOs[0] == 1 && xSAndOs[1] == 1 && xSAndOs[2] == 1) || (xSAndOs[3] == 1 && xSAndOs[4] == 1 && xSAndOs[5] == 1) || (xSAndOs[6] == 1 && xSAndOs[7] == 1 && xSAndOs[8] == 1) || (xSAndOs[0] == 1 && xSAndOs[3] == 1 && xSAndOs[6] == 1) || (xSAndOs[1] == 1 && xSAndOs[4] == 1 && xSAndOs[7] == 1) || (xSAndOs[2] == 1 && xSAndOs[5] == 1 && xSAndOs[8] == 1) || (xSAndOs[0] == 1 && xSAndOs[4] == 1 && xSAndOs[8] == 1) || (xSAndOs[2] == 1 && xSAndOs[4] == 1 && xSAndOs[6] == 1)){
  31.         timesYouWon++; Reset("X Won"); }
  32.     else if ((xSAndOs[0] == 2 && xSAndOs[1] == 2 && xSAndOs[2] == 2) || (xSAndOs[3] == 2 && xSAndOs[4] == 2 && xSAndOs[5] == 2) || (xSAndOs[6] == 2 && xSAndOs[7] == 2 && xSAndOs[8] == 2) || (xSAndOs[0] == 2 && xSAndOs[3] == 2 && xSAndOs[6] == 2) || (xSAndOs[1] == 2 && xSAndOs[4] == 2 && xSAndOs[7] == 2) || (xSAndOs[2] == 2 && xSAndOs[5] == 2 && xSAndOs[8] == 2) || (xSAndOs[0] == 2 && xSAndOs[4] == 2 && xSAndOs[8] == 2) || (xSAndOs[2] == 2 && xSAndOs[4] == 2 && xSAndOs[6] == 2))
  33.         Reset("O Won");
  34.     else if (xSAndOs[0] != 0 && xSAndOs[1] != 0 && xSAndOs[2] != 0 &&  xSAndOs[3] != 0 && xSAndOs[4] != 0 && xSAndOs[5] != 0 &&  xSAndOs[6] != 0 && xSAndOs[7] != 0 && xSAndOs[8] != 0 )
  35.         Reset("Draw");
  36.     if (whosTurnIsIt == 1) { // cps turn
  37.         oBlockedYou = 0;
  38.         for (int i = 0; i < 9; i += 3) { // check horizontal
  39.             if (xSAndOs[i] == 1 && xSAndOs[i + 1] == 1 && xSAndOs[i + 2] == 0) {
  40.                 oBlockedYou = 1, boardPos = i + 2; break; }
  41.             else if (xSAndOs[i] == 1 && xSAndOs[i + 1] == 0 && xSAndOs[i + 2] == 1) {
  42.                 oBlockedYou = 1, boardPos = i + 1; break; }
  43.             else if (xSAndOs[i] == 0 && xSAndOs[i + 1] == 1 && xSAndOs[i + 2] == 1) {
  44.                 oBlockedYou = 1, boardPos = i; break; }
  45.         }
  46.         for (int i = 0; i < 3; i++) { // check vertical
  47.             if (xSAndOs[i] == 1 && xSAndOs[i + 3] == 1 && xSAndOs[i + 6] == 0) {
  48.                 oBlockedYou = 1, boardPos = i + 6; break; }
  49.             else if (xSAndOs[i] == 1 && xSAndOs[i + 3] == 0 && xSAndOs[i + 6] == 1) {
  50.                 oBlockedYou = 1, boardPos = i + 3; break; }
  51.             else if (xSAndOs[i] == 0 && xSAndOs[i + 3] == 1 && xSAndOs[i + 6] == 1) {
  52.                 oBlockedYou = 1, boardPos = i; break; }
  53.         }
  54.         if (xSAndOs[0] == 1 && xSAndOs[4] == 1 && xSAndOs[8] == 0) // check diagonal
  55.             oBlockedYou = 1, boardPos = 8;
  56.         else if (xSAndOs[0] == 1 && xSAndOs[4] == 0 && xSAndOs[8] == 1)
  57.             oBlockedYou = 1, boardPos = 4;
  58.         else if (xSAndOs[0] == 0 && xSAndOs[4] == 1 && xSAndOs[8] == 1)
  59.             oBlockedYou = 1, boardPos = 0;
  60.         if (xSAndOs[2] == 1 && xSAndOs[4] == 1 && xSAndOs[6] == 0)
  61.             oBlockedYou = 1, boardPos = 6;
  62.         else if (xSAndOs[2] == 1 && xSAndOs[4] == 0 && xSAndOs[6] == 1)
  63.             oBlockedYou = 1, boardPos = 4;
  64.         else if (xSAndOs[2] == 0 && xSAndOs[4] == 1 && xSAndOs[6] == 1)
  65.             oBlockedYou = 1, boardPos = 2; 
  66.         if (!oBlockedYou)
  67.             do {
  68.                 spotIsTaken = xSAndOs[(boardPos = (rand() % 9))] != 0;
  69.             } while (spotIsTaken);
  70.         xSAndOs[boardPos] = 2;
  71.         whosTurnIsIt = !whosTurnIsIt;
  72.     }
  73.     else if (!whosTurnIsIt && !mouseButton && mouseClickState) // our turn
  74.         if (xSAndOs[(mouseX / 200) + ((mouseY / 150) * 3)] == 0) {
  75.             xSAndOs[(mouseX / 200) + ((mouseY / 150) * 3)] = (whosTurnIsIt + 1);
  76.             whosTurnIsIt = !whosTurnIsIt;
  77.         }
  78.     DrawTexture(gameBoardTextureId, 0, 0, 56, 56);
  79.     for (int y = 0; y < 3; y++)
  80.         for (int x = 0; x < 3; x++)
  81.             if (xSAndOs[x + (y * 3)])
  82.                 DrawTexture(xSAndOs[x + (y * 3)], ((56 / 3) * x) + (x + 1), ((56 / 3) * y) + (y + 1), 16, 16);
  83.     glutSwapBuffers();
  84.     mouseButton = 0, mouseClickState = 0, mouseX = 0, mouseY = 0;
  85. }
  86. void MouseEvent(int button, int state, int x, int y) { mouseButton = button, mouseClickState = state, mouseX = x, mouseY = y; }
  87. void main(int argc, char **argv)  {
  88.     glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA); glutInitWindowSize(640, 480); // inits
  89.     window = glutCreateWindow("Gl Tic Tac Toe"); // window creation
  90.     glutDisplayFunc(&DrawGLScene); glutIdleFunc(&DrawGLScene); glutMouseFunc(&MouseEvent); // glut events
  91.     MakeTexture(gameSpriteX, 16, 16, &gameSpriteXTextureId); MakeTexture(gameSpriteO, 16, 16, &gameSpriteOTextureId); MakeTexture(gameBoard, 56, 56, &gameBoardTextureId); // make textures
  92.     glMatrixMode(GL_PROJECTION); glOrtho(0.0f, 56, 56, 0.0f, 0.0f, 1); glMatrixMode(GL_MODELVIEW); // viewport setup
  93.     glEnable(GL_TEXTURE_2D); srand(time(0)); // enable textures and seeding random numbers
  94.     glutMainLoop(); // start glut main loop
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement