Important alert: (current site time 11/28/2014 8:54:08 PM EDT)
 

article

TIC TAC TOE

Email
Submitted on: 2/25/2013 3:41:58 PM
By: guideX  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 4241
author picture
(About the author)
 
     A simple game of Tic Tac Toe I wrote

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				// TIC TAC TOE GAME
#include 
#include 
#include "games_common.h"
char _squares[9];
void InitSquares() {
	_squares[0] = '1';
	_squares[1] = '2';
	_squares[2] = '3';
	_squares[3] = '4';
	_squares[4] = '5';
	_squares[5] = '6';
	_squares[6] = '7';
	_squares[7] = '8';
	_squares[8] = '9';
}
void PrintBoard() {
	char _value1[6] = { _squares[0], '|', _squares[1], '|', _squares[2], '\0' };
	char _value2[6] = { _squares[3], '|', _squares[4], '|', _squares[5], '\0' };
	char _value3[6] = { _squares[6], '|', _squares[7], '|', _squares[8], '\0' };
	char _seperator[6] = { '-', '+', '-', '+', '-', '\0' };
	print(_value1);
	print(_seperator);
	print(_value2);
	print(_seperator);
	print(_value3);
	print(_seperator);
}
void tic_tac_toe() {
	char _play_again;
	char _player_mark;
	bool _win_game = false;
	bool _game_over = false;
	bool _valid_move = false;
	int _player_turn = 1;
	int _next_move;
	InitSquares();
	do {
		if(_player_turn == 1) {
			_player_mark = 'X';
		} else {
			_player_mark = 'O';
		}
		PrintBoard();
		std::cout << "Player: " << _player_turn << "'s move" << std::endl;
		do {
			std::cin >> _next_move;
			_valid_move = false;
			for(int i = 0; i <= 9; ++i) {
				if(ConvertIntToChar(_next_move) == ConvertIntToChar(i) && _squares[i -1] == ConvertIntToChar(_next_move)) {
					_squares[i - 1] = _player_mark;
					_valid_move = true;
					break;
				}
			}
			if(_valid_move == false) {
				std::cout << "Invalid Move. Try again." << std::endl;
			}
		} while (!_valid_move);
		_game_over = false;
		_win_game = true;
		if (_squares[0] != '1') {
			if (_squares[1] == _squares[0] && _squares[2] == _squares[0]) {
				_game_over = true;
			}
			if (_squares[3] == _squares[0] && _squares[6] == _squares[0]) {
				_game_over = true;
			}
		}
		if (_squares[4] != '5') {
			if (_squares[0] == _squares[4] && _squares[8] == _squares[4]) {
				_game_over = true;
			}
			if (_squares[1] == _squares[4] && _squares[7] == _squares[4]) {
				_game_over = true;
			}
			if (_squares[3] == _squares[4] && _squares[5] == _squares[4]) {
				_game_over = true;
			}
			if (_squares[2] == _squares[4] && _squares[6] == _squares[4]) {
				_game_over = true;
			}
		}
		if (_squares[8] != '9') {
			if (_squares[2] == _squares[8] && _squares[5] == _squares[8]) {
				_game_over = true;
			}
			if (_squares[6] == _squares[8] && _squares[7] == _squares[8]) {
				_game_over = true;
			}
		}
		if (_squares[0] != '1' && _squares[1] != '2' && _squares[2] != '3' && _squares[3] != '4' && _squares[4] != '5' && _squares[5] != '6' && _squares[6] != '7' && _squares[7] != '8' && _squares[8] != '9' && !_game_over) {
			_game_over = true;
			_win_game = false;
		}
		if (_game_over) {
			if (_win_game) {
				std::cout << "Player" << _player_turn << " wins!" << std::endl;
			}
			PrintBoard();
			std::cout << "Game Over!" << std::endl;
			std::cout << "Play again (y/n)?" << std::endl;
			std::cin >> _play_again;
			if (_play_again == 'y') {
				_game_over = false;
				InitSquares();
			}
			_player_turn = 1;
		} else {
			if (_player_turn == 1) {
				_player_turn = 2;
			} else {
				_player_turn = 1;
			}
		}
	} while (!_game_over);
}
bool _initialized_time = false;
void Wait() {
	std::cout << "Prease any key to continue ..." << std::endl;
	std::cin.get();
}
void print(char* _value) {
	std::cout << _value << std::endl;
}
char ConvertIntToChar(int _int) {
	return (char)(((int)'0')+_int);
}
int ReturnRandomNumber(int _start, int _end) {
	if(_initialized_time == false) {
		_initialized_time = true;
		srand(time(0));
	}
	int n = rand() % _end + _start;
	return n;
}
void ClearConsoleWindow() {
	for (int i = 0; i < 40; i++) {
		std::cout << " ";
	}
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/12/2013 6:58:26 AMJung

hi! tks for your post. But i can't find file games_common.h. Can you help me? tks
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.