Quick Search for:  in language:    
Just,another,simple,Game
   Code/Articles  |  Newest/Best  |  Community  |  Jobs  |  Other  |  Goto  | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 681,728. lines
 Jobs: 96. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for C/ C++.
Yet another Console Space Invaders
By Steven McElrea on 11/28


PrimeTester
By Titus Laska on 11/28


Click here to see a screenshot of this code!Commander
By Hylke Donker on 11/27

(Screen Shot)

Game Trees and minimax method (minima maxima)
By Hasan Alayli on 11/27


Origin of Computer Games!
By suneet singh on 11/27


CD Tray Open
By pr0ger on 11/27


Click here to see a screenshot of this code!Maze solver using backtacking & recursion
By Hasan Alayli on 11/27

(Screen Shot)

Grand TicAttackToy
By Divine Light on 11/27


Word Deriver thru stack operations
By Sayash Kumar on 11/26


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

TicTacToe(single)

Print
Email
 
VB icon
Submitted on: 11/17/2003 5:18:38 PM
By: Gummi  
Level: Beginner
User Rating: Unrated
Compatibility:C++ (general)

Users have accessed this code 440 times.
 
(About the author)
 
     Just another simple Tic Tac Toe Game

 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

//**************************************
//     
//INCLUDE files for :TicTacToe(single)
//**************************************
//     
iostream.h
stdlib.h
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code 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 code (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 code 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 code or code's description.

//**************************************
//     
// Name: TicTacToe(single)
// Description:Just another simple Tic T
//     ac Toe Game
// By: Gummi
//
// Inputs:Input keys (1-9)
//
// Returns:display board to screen
//
// Assumes:Need to know basic I/O statem
//     ents and arrays
//
// Side Effects:This program was compile
//     d using the free version Dev-C++ v4.0, t
//     ested on Win98, untested on other compil
//     ers and platform
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=7136&lngWId;=3//for details.//**************************************
//     

#include <iostream.h>
#include <stdlib.h> //needed for rand
#include <windows.h>
void draw_board();
void player_movement();
void computer_movement();
bool check_for_winner(char player);
char board_info[9] = {'1','2','3',
'4','5','6',
'7','8','9'};
int main()
    {
    int counter = 0;
    bool winner = false;
    srand(GetTickCount()); //sets randomizer as time *unique*
    draw_board();
    for (int i = 0; i < 9 ; i++)
        {
        board_info[i] = ' ';
    }

draw_board(); //game loop while (true) { player_movement(); draw_board(); winner = check_for_winner('X'); counter++; if (winner) { cout << "\nYou Win" << endl; break; }
if (counter >= 9) { cout << "\nDraw Game" << endl; break; }
computer_movement(); draw_board(); winner = check_for_winner('O'); counter++; if (winner) { cout << "\nComputer Win" <<endl; break; }
}
system("PAUSE"); return 0; }
void draw_board() { cout << endl; for (int i = 0; i < 9; i++) { cout << " | " << board_info[i]; if ((i+1) % 3 == 0) cout << endl << " -------------" <<endl; }
}
void player_movement() { int choice; cout << "\nPlease select a number from 1 - 9: "; cin >> choice; //Error Checking if (cin.fail()) { cout << "Error!"; exit(1); }
while (choice >= 10 || choice <=0) { cout << "\nPlease select again: "; cin >> choice; }
while (board_info[(choice-1)] != ' ') { cout << "\nPlease select again: "; cin >> choice; };
board_info[choice-1] = 'X'; }
void computer_movement() { if ((int(board_info[0]) + int(board_info[1]) + int(board_info[2])) == (2*int(char('X')))+int(char(' '))) { if (board_info[0] == ' ') board_info[0] = 'O'; else if (board_info[1] == ' ') board_info[1] = 'O'; else if (board_info[2] == ' ') board_info[2] = 'O'; }
else if ((int(board_info[3]) + int(board_info[4]) + int(board_info[5])) == (2*int(char('X')))+int(char(' '))) { if (board_info[3] == ' ') board_info[3] = 'O'; else if (board_info[4] == ' ') board_info[4] = 'O'; else if (board_info[5] == ' ') board_info[5] = 'O'; }
else if ((int(board_info[6]) + int(board_info[7]) + int(board_info[8])) == (2*int(char('X')))+int(char(' '))) { if (board_info[6] == ' ') board_info[6] = 'O'; else if (board_info[7] == ' ') board_info[7] = 'O'; else if (board_info[8] == ' ') board_info[8] = 'O'; }
else if ((int(board_info[0]) + int(board_info[3]) + int(board_info[6])) == (2*int(char('X')))+int(char(' '))) { if (board_info[0] == ' ') board_info[0] = 'O'; else if (board_info[3] == ' ') board_info[3] = 'O'; else if (board_info[6] == ' ') board_info[6] = 'O'; }
else if ((int(board_info[1]) + int(board_info[4]) + int(board_info[7])) == (2*int(char('X')))+int(char(' '))) { if (board_info[1] == ' ') board_info[1] = 'O'; else if (board_info[4] == ' ') board_info[4] = 'O'; else if (board_info[7] == ' ') board_info[7] = 'O'; }
else if ((int(board_info[2]) + int(board_info[5]) + int(board_info[8])) == (2*int(char('X')))+int(char(' '))) { if (board_info[2] == ' ') board_info[2] = 'O'; else if (board_info[5] == ' ') board_info[5] = 'O'; else if (board_info[8] == ' ') board_info[8] = 'O'; }
else if ((int(board_info[0]) + int(board_info[4]) + int(board_info[8])) == (2*int(char('X')))+int(char(' '))) { if (board_info[0] == ' ') board_info[0] = 'O'; else if (board_info[4] == ' ') board_info[4] = 'O'; else if (board_info[8] == ' ') board_info[8] = 'O'; }
else if ((int(board_info[2]) + int(board_info[4]) + int(board_info[6])) == (2*int(char('X')))+int(char(' '))) { if (board_info[2] == ' ') board_info[2] = 'O'; else if (board_info[4] == ' ') board_info[4] = 'O'; else if (board_info[6] == ' ') board_info[6] = 'O'; }
else { int number = rand()%9; while (board_info[number] != ' ') number = rand()%9; board_info[number] = 'O'; }
}
bool check_for_winner(char player) { if ((board_info[0] == player && board_info[1] == player) && (board_info[2] == player)) return true; if ((board_info[3] == player && board_info[4] == player) && (board_info[5] == player)) return true; if ((board_info[6] == player && board_info[7] == player) && (board_info[8] == player)) return true; if ((board_info[0] == player && board_info[3] == player) && (board_info[6] == player)) return true; if ((board_info[1] == player && board_info[4] == player) && (board_info[7] == player)) return true; if ((board_info[2] == player && board_info[5] == player) && (board_info[8] == player)) return true; if ((board_info[0] == player && board_info[4] == player) && (board_info[8] == player)) return true; if ((board_info[2] == player && board_info[4] == player) && (board_info[6] == player)) return true; else return false; }

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.