Thread: Tic Tac Toe display in Code::Blocks

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Georgia
    Posts
    1

    Tic Tac Toe display in Code::Blocks

    I am trying to make a better display than the one char extremely small display you get with most tic tac toe games. However, my goal is to achieve this without SDL or the like. Which leaves me using a technique where i have to print arrays and change points within the array one-by-one to display what I want. Here is my code.

    Code:
    //This is my Display Test code for TACTOE.
    
    
    
    
    #include<iostream>
    #include<stdlib.h>
    
    int main()
    {
    
    //Initializes the array and my variable
    char ArrayOne[21][21];
    int counter=1;
    
    
    //Loop that initializes the array to all zeros
    for (int i=1; i<21; i++)
    {
        for (int l=1; l<21; l++)
        {
            ArrayOne[i][l]=0;
    
        }}
    
    //Loop that draws the boarders
    for (int i=7; i<21; i+=7)
    {
        for (int l=1; l<21; l++)
        {
            ArrayOne[i][l]=219;
            ArrayOne[l][i]=219;
        }}
    
    
    //This is where the numbers for the board begin
    //each point is changed manually.
    
    //Number 1
        ArrayOne[2][3]=222;
        ArrayOne[2][4]=221;
        ArrayOne[3][3]=222;
        ArrayOne[3][4]=221;
        ArrayOne[4][3]=222;
        ArrayOne[4][4]=221;
        ArrayOne[5][3]=222;
        ArrayOne[5][4]=221;
    
    //Number 2
        ArrayOne[1][10]=220;
        ArrayOne[1][11]=220;
        ArrayOne[2][9]=219;
        ArrayOne[2][12]=219;
        ArrayOne[3][11]=223;
        ArrayOne[3][10]=220;
        ArrayOne[4][9]=219;
        ArrayOne[5][9]=219;
        ArrayOne[5][10]=220;
        ArrayOne[5][11]=220;
        ArrayOne[5][12]=220;
    
    //Number 3
        ArrayOne[1][17]=220;
        ArrayOne[1][18]=220;
        ArrayOne[2][16]=219;
        ArrayOne[2][19]=219;
        ArrayOne[3][18]=219;
        ArrayOne[3][17]=220;
        ArrayOne[4][19]=219;
        ArrayOne[5][19]=223;
        ArrayOne[5][18]=220;
        ArrayOne[5][17]=220;
        ArrayOne[5][16]=219;
    
    //Number 4
        ArrayOne[9][2]=219;
        ArrayOne[10][2]=219;
        ArrayOne[11][2]=219;
        ArrayOne[11][3]=220;
        ArrayOne[11][4]=219;
        ArrayOne[10][4]=219;
        ArrayOne[11][5]=220;
        ArrayOne[12][4]=219;
        ArrayOne[13][4]=223;
    
    //Number 5
        ArrayOne[8][9]=220;
        ArrayOne[8][10]=220;
        ArrayOne[8][11]=220;
        ArrayOne[8][12]=220;
        ArrayOne[9][9]=219;
        ArrayOne[10][9]=219;
        ArrayOne[10][10]=220;
        ArrayOne[10][11]=220;
        ArrayOne[10][12]=220;
        ArrayOne[11][12]=219;
        ArrayOne[12][12]=223;
        ArrayOne[12][11]=220;
        ArrayOne[12][10]=220;
        ArrayOne[12][9]=220;
    
    //Number 6
        ArrayOne[8][18]=220;
        ArrayOne[9][17]=219;
        ArrayOne[10][16]=219;
        ArrayOne[11][16]=219;
        ArrayOne[12][16]=223;
        ArrayOne[12][17]=220;
        ArrayOne[12][18]=220;
        ArrayOne[12][19]=223;
        ArrayOne[11][19]=220;
        ArrayOne[11][18]=223;
        ArrayOne[11][17]=223;
    
    //Number 7
        ArrayOne[15][2]=220;
        ArrayOne[15][3]=220;
        ArrayOne[15][4]=220;
        ArrayOne[15][5]=220;
        ArrayOne[16][5]=219;
        ArrayOne[17][4]=219;
        ArrayOne[18][4]=219;
        ArrayOne[19][4]=219;
    
    //Number 8
        ArrayOne[15][10]=220;
        ArrayOne[15][11]=220;
        ArrayOne[16][9]=219;
        ArrayOne[16][12]=219;
        ArrayOne[17][10]=219;
        ArrayOne[17][11]=219;
        ArrayOne[18][9]=219;
        ArrayOne[18][12]=219;
        ArrayOne[19][9]=223;
        ArrayOne[19][12]=223;
        ArrayOne[19][10]=220;
        ArrayOne[19][11]=220;
    
    //Number 9
        ArrayOne[15][17]=220;
        ArrayOne[15][18]=220;
        ArrayOne[16][16]=219;
        ArrayOne[16][19]=219;
        ArrayOne[17][16]=223;
        ArrayOne[17][17]=220;
        ArrayOne[17][18]=220;
        ArrayOne[17][19]=219;
        ArrayOne[18][19]=219;
        ArrayOne[19][19]=219;
    
    //Beginning of the Game Loop
    for (int m=1; m<9; m++)
    {
        system("cls");
    
    //Loop that Updates the Board
    for (int i=1; i<21; i++)
    {
        for (int l=1; l<21; l++)
        {
            if (counter==21)
            {
                std::cout << "\n";
                counter=1;
            }
            std::cout << ArrayOne[i][l];
            counter++;
        }}
    
    std::cout << "\n\n";
    std::cout << "Please enter a number.....";
    std::cin.ignore();      //does nothing, no reason to input yet
    
    //This test that a number can be replaced by either a X or an O
    
    //First we clear number 1
        ArrayOne[2][3]=0;
        ArrayOne[2][4]=0;
        ArrayOne[3][3]=0;
        ArrayOne[3][4]=0;
        ArrayOne[4][3]=0;
        ArrayOne[4][4]=0;
        ArrayOne[5][3]=0;
        ArrayOne[5][4]=0;
    //Draws X
        ArrayOne[1][1]=222;
        ArrayOne[2][2]=219;
        ArrayOne[3][3]=222;
        ArrayOne[4][5]=219;
        ArrayOne[5][6]=221;
        ArrayOne[1][6]=221;
        ArrayOne[2][5]=219;
        ArrayOne[4][2]=219;
        ArrayOne[5][1]=222;
        ArrayOne[3][4]=221;
    
    //Number 2 Clear
        ArrayOne[1][10]=0;
        ArrayOne[1][11]=0;
        ArrayOne[2][9]=0;
        ArrayOne[2][12]=0;
        ArrayOne[3][11]=0;
        ArrayOne[3][10]=0;
        ArrayOne[4][9]=0;
        ArrayOne[5][9]=0;
        ArrayOne[5][10]=0;
        ArrayOne[5][11]=0;
        ArrayOne[5][12]=0;
    
    //Draws O
        ArrayOne[1][10]=220;
        ArrayOne[1][11]=220;
        ArrayOne[2][9]=219;
        ArrayOne[2][12]=219;
        ArrayOne[3][9]=219;
        ArrayOne[3][12]=219;
        ArrayOne[4][9]=219;
        ArrayOne[4][12]=219;
        ArrayOne[5][9]=219;
        ArrayOne[5][12]=219;
        ArrayOne[6][11]=223;
        ArrayOne[6][10]=223;
    
    }
    
    
    
    }
    My question is, Is there a better way other that using SDL or the like.

    I couldn't find a tutorial on ASCII games, maybe if someone could point me in that direction.

    Thanks a bunch

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Why don't you want to use a graphics library then? in any case i ran this and was impressed by your efforts, just on the basis it must have been such a headache to sort out that formatting haha, it looks kind of neat though.

    if you insist on this type of rendering then for something like this the improvements will probably come from code organisation to allow you to build on the game so far, move the initialisation loops to a function, move the refresh routine to a function , the symbol drawing in a function etc.
    As far as actual display goes you might want to experiment using a SetConsoleCursorPosition() function to write the chars at specific coords to create your game images i think the difference here would be negligible though. the curses text library has neat features for text based apps that will let you dress up the output, but then if you are going to learn that you may as well learn a graphics library while you are at it.....
    Last edited by rogster001; 03-04-2010 at 03:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe check winner
    By dhardin in forum C++ Programming
    Replies: 15
    Last Post: 12-20-2009, 07:57 PM
  2. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Tic Tac Toe display problem
    By Munkey01 in forum Game Programming
    Replies: 9
    Last Post: 03-01-2003, 06:35 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM