C/C++ Tic-Tac-Toe Console Game Log

Discussion in 'Programming General' started by Vanack, Feb 19, 2009.

C/C++ Tic-Tac-Toe Console Game Log
  1. Unread #1 - Feb 19, 2009 at 1:23 AM
  2. Vanack
    Joined:
    Jul 17, 2008
    Posts:
    470
    Referrals:
    0
    Sythe Gold:
    0

    Vanack Forum Addict

    C/C++ Tic-Tac-Toe Console Game Log

    Tic-Tac-Toe Basic Console Game Log

    I've recently started taking classes at North Western on C++/C programming and this is round 1 with Tic-Tac-Toe basic Console game.

    I've made this is Microsoft Visual Studio Academic Edition. I've been taking classes regularly and am learning a large amount on this coding language. I wrote this up in less than 2 hours.

    This is my very first attempt on Console Based Games and I'll be updating this constantly as it's not working as it should.

    All it's currently doing is recreating the 6x6 grid [though it only seems to be a 3x3 grid] and every time shows the place where you moved, the the computer finds the next open spot in the grid and fills that.

    You however, can pick any spot in the 3x3 grid. The computer has no mental capability of it's own to date, and should be updated in the next few days based on my schedule.

    Enjoy ;)


    Please, rate the current code /10 and give feedback. IF you don't have any comment on my coding it will be considered spam and reported.



    Code:
    // Attempt 1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    
    #define XWINS 1
    #define OWINS 2
    #define TIE   3
    
    using namespace std;
    
    void Draw_Board();
    int  DidAnyoneWin();
    int  AnySpotsLeft();
    int  MyTurn(char);
    
    // Define 6 x 6 Grid and insert symbols in the array
    char Board[][8] = {" 1 2 3 ", "1 | | ", " - - - ", "2 | |  ", " - - - ", "3 | |  "};
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int	i, j, Row=0, Column=0;
    	char XorO  = NULL;
    	char other = NULL;
    	int  done  = false;
    
    
    	
    	// Initialize grid
    	for (i=1; i<6; i+=2)
    		for (j=1; j<6; j+=2)
    			Board[i][j]=NULL;
    		
    	// Take credit
    	cout << "Tic Tac Toe - By xaixiax Vanack\n" << endl << endl;
    
    	//Print Board
    	Draw_Board();
    
    	do 
    	{
    		// Ask whether they are X's or O's
    		cout << "Are you X or O?\n";
    		cin >> XorO;
    		cout << endl << endl;
    		
    		// Capitalize, if necessary
    		if (XorO == 'x') 
    			XorO = 'X';
    		else if (XorO == 'o') 
    			XorO = 'O';
    	} while (XorO != 'X' && XorO != 'O'); // make sure they only use X's and O's
    	
    	// What am I?
    	if (XorO == 'X') other = 'O';
    	else other = 'X';
    
    	do 
    	{
    		// Ask where they want theirs to go
    		cout << "What row do you want to place your piece in?\n";
    		cin >> Row;
    		cout << endl;
    		cout << "What column do you want to place your piece in?\n" << endl;
    		cin >> Column;
    		cout << endl;
    
    		// Compensate for Board layout
    		Row = 2 * Row -1;
    		Column = 2 * Column -1;
    
    		// Make sure space not already taken
    		if (Board[Row][Column]== 'X' || Board[Row][Column]== 'O')
    		{
    			cout << endl << " That spot is taken. You're a cheater so I win.\n" << endl << endl;
    			break;
    		}	else Board[Row][Column]=XorO;
    
    		// Draw # w new mark
    		Draw_Board();
    
    		// Check for 3 in row , if so => done
    		done = DidAnyoneWin();
    		
    		// If no empty spot => tie + done
    		if (!done) done = AnySpotsLeft();
    
    		if (!done)
    		{
    			// Place your mark in empty spot
    			MyTurn(other);
    
    			// If no empty spot => tie + done
    			done = AnySpotsLeft();
    
    			// Draw # w new mark
    			Draw_Board();
    
    			// Check for 3 in row or tie
    			done = DidAnyoneWin();
    		}
    	} while (!done);
    
    	switch (done)
    	{
    		case XWINS:
    			if (XorO == 'X')
    				cout << "Darn! You beat me!\n\n";
    			else cout << "In your face, I win!\n\n";
    			break;
    		case OWINS:
    			if (XorO == 'O')
    				cout << "Darn! You beat me!\n\n";
    			else cout << "In your face, I win!\n\n";
    			break;
    		case TIE:
    			cout << "Yuck! This is like kissing your sister!\n\n";
    			break;
    		default:
    			cout << "Game over ... Cheater.\n" << endl;
    	}
    	cout << "\n\nHit enter to finish\n" << endl;
    	cin >> Column;
    
    	return 0;
    }
    
    void Draw_Board()
    {
    	int i=0, j=0;
    
    	cout << endl;
    	
    	for (i=0; i<6; i++)
    	{
    		cout << "       ";
    		for (j=0; j<6; j++)
    			cout << Board[i][j];
    		cout << endl;
    	}
    	cout << endl;
    }
    
    int DidAnyoneWin()
    {
    	int row=0, column=0;
    	int winner = 0;
    
    	// Any horizontal winners?
    	column = 1;
    	for (row=1; row<6; row+=2)
    		if (Board[row][column]== Board[row][column+2] && Board[row][column+2]== Board[row][column+4]
    				&& Board[row][column]!= NULL)
    			if (Board[row][column]== 'X')
    				winner=XWINS;
    			else winner=OWINS;
    
    	// Any vertical winners?
    	row = 1;
    	for (column=1; column<6; column+=2)
    		if (Board[row][column]== Board[row+2][column] && Board[row+2][column]== Board[row+4][column]
    					&& Board[row][column]!= NULL)
    				if (Board[row][column]== 'X')
    				winner=XWINS;
    			else winner=OWINS;
    
    	// Check descending vertical
    	row    = 1;
    	column = 1;
    	if (Board[row][column]== Board[row+2][column+2] && Board[row+2][column+2]== Board[row+4][column+4]
    			&& Board[row][column]!= NULL)
    		if (Board[row][column]== 'X')
    				winner=XWINS;
    		else winner=OWINS;
    
    	// Check ascending vertical
    	row    = 5;
    	column = 1;
    	if (Board[row][column]== Board[row-2][column+2] && Board[row-2][column+2]== Board[row-4][column+4]
    			&& Board[row][column]!= NULL)
    		if (Board[row][column]== 'X')
    				winner=XWINS;
    		else winner=OWINS;
    
    	return winner;
    }
    
    int AnySpotsLeft()
    {
    	int row=0, column=0;
    	int full = TIE;
    
    	// Is there an empty space?
    	for (row=1; row<6; row+=2)
    	{
    		for (column=1; row<6; row+=2)
    			if (Board[row][column]== NULL) 
    			{
    				full = false;
    				break;
    			}
    		if (full = false) break;
    	}
    
    	return full;
    }
    
    MyTurn(char MyChar)
    {
    	int row=0, column=0;
    	
    	// find an empty space?
    	for (row=1; row<6; row+=2)
    		for (column=1; column<6; column+=2)
    			if (Board[row][column]== NULL) 
    			{
    				Board[row][column]= MyChar;
    				row = 10;
    				break;
    			}
    
    	return 0;
    }
    
    
    
     
  3. Unread #2 - Mar 11, 2009 at 9:06 PM
  4. zitbam
    Joined:
    Jan 3, 2009
    Posts:
    3
    Referrals:
    0
    Sythe Gold:
    0

    zitbam Newcomer

    C/C++ Tic-Tac-Toe Console Game Log

    I don't know how to code C++ or C, but that looks like that would take a heck of time to make that. I script LUA, LUA is based on C. :)
     
  5. Unread #3 - Mar 14, 2009 at 10:48 AM
  6. Esuom
    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0

    Esuom Guru
    Banned

    C/C++ Tic-Tac-Toe Console Game Log

    I can't wait until I can take a C++ class in college. The 18 week highschool class that took me 4 weeks just don't cut it.
     
  7. Unread #4 - Mar 17, 2009 at 3:50 PM
  8. mopar-user
    Joined:
    Jul 23, 2007
    Posts:
    1,188
    Referrals:
    1
    Sythe Gold:
    0

    mopar-user Guru
    Banned

    C/C++ Tic-Tac-Toe Console Game Log

    I'm in a year long C++ class and some of that I don't know. GL with it.
     
  9. Unread #5 - Apr 28, 2009 at 9:10 PM
  10. Aye Pod
    Joined:
    Aug 24, 2008
    Posts:
    240
    Referrals:
    1
    Sythe Gold:
    0

    Aye Pod Active Member
    Banned

    C/C++ Tic-Tac-Toe Console Game Log

    i tried saving it as a .bat file.. and it doesn't work properly
     
  11. Unread #6 - Apr 29, 2009 at 4:19 AM
  12. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    C/C++ Tic-Tac-Toe Console Game Log

    That is because C++ isn't Windows Batch. That's like comparing a pea to a watermelon. C++ is the watermelon.

    You need to compile C++ applications.
     
  13. Unread #7 - May 4, 2009 at 2:14 AM
  14. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,071
    Referrals:
    465
    Sythe Gold:
    5,271
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    C/C++ Tic-Tac-Toe Console Game Log

    Just so you know, that's entirely procedural C. Aside from being compiled with a C++ compiler (C is a subset of C++) it has virtually nothing to do with C++.
     
  15. Unread #8 - May 4, 2009 at 8:50 PM
  16. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    C/C++ Tic-Tac-Toe Console Game Log

    Except for the method of input/output used (iostream) seeing as it is a part of C++. I guess you are correct since those could easily be changed to be scanf() and printf() from C.
     
  17. Unread #9 - May 16, 2009 at 7:27 AM
  18. TeamEvox
    Joined:
    Mar 28, 2009
    Posts:
    535
    Referrals:
    0
    Sythe Gold:
    0

    TeamEvox Forum Addict
    Banned

    C/C++ Tic-Tac-Toe Console Game Log

    Looks like a lot of work, nice job 9/10
     
< Basic help | [CS 1.6] Simple Redraw ESP >

Users viewing this thread
1 guest


 
 
Adblock breaks this site