• Log in or sign up for an account here: https://www.sythe.org/login

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

Vanack

Forum Addict
Joined
Jul 17, 2008
Sythe Gold
0
Messages
470
Age
35
Location
Italy
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;
}
 
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.
 
I'm in a year long C++ class and some of that I don't know. GL with it.
 
i tried saving it as a .bat file.. and it doesn't work properly
 
Member of the Month Winner
Sythe's 10th Anniversary
i tried saving it as a .bat file.. and it doesn't work properly
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.
 
Clefairy
Supporting Business
Discord Voice
Jigglypuff
Hell yeah boooi
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
Tier 3 Prizebox
Toast Wallet User
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++.
 
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++.
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.
 
Runescape Gamblingrunescape gambling
Back
Top