Simple Tic Tac Toe Game - Muhammad Faruq Nuruddinsyah
Simple Tic Tac Toe Game

Here's Simple Tic Tac Toe Game Algorithm in C++ (Human vs Computer).

Source Code:
/* 
 * File:   tictactoe.cpp
 * Author: Muhammad Faruq Nuruddinsyah
 *
 * Created on May 5, 2013, 4:25 AM
 */
 
#include <iostream>
#include <stdlib.h>
#include <time.h>
 
using namespace std;
 
char box[10];
bool game_over;
int rand_pos;
 
void reset_game();
void loop_game();
void reset_board();
void print_board();
void fill_box(int, char);
void user_input();
void check();
 
bool is_user_wins();
bool is_computer_wins();
bool is_draw();
bool computer_attack();
bool computer_defense();
 
void computer_use_random();
void user_wins();
void computer_wins();
void draw();
 
int main(){
    srand(time(NULL));
 
    cout << "Welcome to Simple TicTacToe 3x3 - Faruq @ 2013\n\n";
    reset_game();
    loop_game();
 
    return 0;
}
 
void reset_game(){
    rand_pos = rand() % 3;
    game_over = false;
    reset_board();
}
 
void reset_board(){
    for(int i = 1; i <= 9; i++){
        fill_box(i, '-');
    }
}
 
void loop_game(){
    print_board();
    if(!game_over) user_input();
}
 
void fill_box(int i, char c){
    box[i] = c;
}
 
void print_board(){
    for(int i = 1; i <= 9; i++){
        if(box[i] != '-'){
            cout << "[" << box[i] << "]";
        }else{
            cout << "[" << i << "]";
        }
 
        if(i % 3 == 0) cout << endl;
    }
}
 
void user_input(){
    int u;
 
    cout << "Masukkan angka: ";
    cin >> u;
 
    if(u < 1 || u > 9){
        cout << "Masukkan harus antara 1 s.d. 9. ";
        user_input();
    }else if(box[u] != '-'){
        cout << "Box telah terisi. ";
        user_input();
    }else{
        fill_box(u, 'X');
        check();
    }
}
 
void check(){
    if(is_user_wins()){
        user_wins();
    }else{
        if(!computer_attack()){
            if(!computer_defense()){
                computer_use_random();
            }
        }
        if(is_computer_wins()){
            computer_wins();
        }else{
            if(is_draw()) draw();
        }
    }
 
    loop_game();
}
 
bool computer_attack(){
    if(box[1] == 'O' && box[2] == 'O' && box[3] == '-')      {fill_box(3, 'O'); return true;}
    else if(box[1] == 'O' && box[3] == 'O' && box[2] == '-') {fill_box(2, 'O'); return true;}
    else if(box[2] == 'O' && box[3] == 'O' && box[1] == '-') {fill_box(1, 'O'); return true;}
 
    else if(box[4] == 'O' && box[5] == 'O' && box[6] == '-') {fill_box(6, 'O'); return true;}
    else if(box[4] == 'O' && box[6] == 'O' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'O' && box[6] == 'O' && box[4] == '-') {fill_box(4, 'O'); return true;}
 
    else if(box[7] == 'O' && box[8] == 'O' && box[9] == '-') {fill_box(9, 'O'); return true;}
    else if(box[7] == 'O' && box[9] == 'O' && box[8] == '-') {fill_box(8, 'O'); return true;}
    else if(box[8] == 'O' && box[9] == 'O' && box[7] == '-') {fill_box(7, 'O'); return true;}
 
    else if(box[1] == 'O' && box[4] == 'O' && box[7] == '-') {fill_box(7, 'O'); return true;}
    else if(box[1] == 'O' && box[7] == 'O' && box[4] == '-') {fill_box(4, 'O'); return true;}
    else if(box[4] == 'O' && box[7] == 'O' && box[1] == '-') {fill_box(1, 'O'); return true;}
 
    else if(box[2] == 'O' && box[5] == 'O' && box[8] == '-') {fill_box(8, 'O'); return true;}
    else if(box[2] == 'O' && box[8] == 'O' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'O' && box[8] == 'O' && box[2] == '-') {fill_box(2, 'O'); return true;}
 
    else if(box[3] == 'O' && box[6] == 'O' && box[9] == '-') {fill_box(9, 'O'); return true;}
    else if(box[3] == 'O' && box[9] == 'O' && box[6] == '-') {fill_box(6, 'O'); return true;}
    else if(box[6] == 'O' && box[9] == 'O' && box[3] == '-') {fill_box(3, 'O'); return true;}
 
    else if(box[1] == 'O' && box[5] == 'O' && box[9] == '-') {fill_box(9, 'O'); return true;}
    else if(box[1] == 'O' && box[9] == 'O' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'O' && box[9] == 'O' && box[1] == '-') {fill_box(1, 'O'); return true;}
 
    else if(box[3] == 'O' && box[5] == 'O' && box[7] == '-') {fill_box(7, 'O'); return true;}
    else if(box[3] == 'O' && box[7] == 'O' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'O' && box[7] == 'O' && box[3] == '-') {fill_box(3, 'O'); return true;}
 
    return false;
}
 
bool computer_defense(){
    if(box[1] == 'X' && box[2] == 'X' && box[3] == '-')      {fill_box(3, 'O'); return true;}
    else if(box[1] == 'X' && box[3] == 'X' && box[2] == '-') {fill_box(2, 'O'); return true;}
    else if(box[2] == 'X' && box[3] == 'X' && box[1] == '-') {fill_box(1, 'O'); return true;}
 
    else if(box[4] == 'X' && box[5] == 'X' && box[6] == '-') {fill_box(6, 'O'); return true;}
    else if(box[4] == 'X' && box[6] == 'X' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'X' && box[6] == 'X' && box[4] == '-') {fill_box(4, 'O'); return true;}
 
    else if(box[7] == 'X' && box[8] == 'X' && box[9] == '-') {fill_box(9, 'O'); return true;}
    else if(box[7] == 'X' && box[9] == 'X' && box[8] == '-') {fill_box(8, 'O'); return true;}
    else if(box[8] == 'X' && box[9] == 'X' && box[7] == '-') {fill_box(7, 'O'); return true;}
 
    else if(box[1] == 'X' && box[4] == 'X' && box[7] == '-') {fill_box(7, 'O'); return true;}
    else if(box[1] == 'X' && box[7] == 'X' && box[4] == '-') {fill_box(4, 'O'); return true;}
    else if(box[4] == 'X' && box[7] == 'X' && box[1] == '-') {fill_box(1, 'O'); return true;}
 
    else if(box[2] == 'X' && box[5] == 'X' && box[8] == '-') {fill_box(8, 'O'); return true;}
    else if(box[2] == 'X' && box[8] == 'X' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'X' && box[8] == 'X' && box[2] == '-') {fill_box(2, 'O'); return true;}
 
    else if(box[3] == 'X' && box[6] == 'X' && box[9] == '-') {fill_box(9, 'O'); return true;}
    else if(box[3] == 'X' && box[9] == 'X' && box[6] == '-') {fill_box(6, 'O'); return true;}
    else if(box[6] == 'X' && box[9] == 'X' && box[3] == '-') {fill_box(3, 'O'); return true;}
 
    else if(box[1] == 'X' && box[5] == 'X' && box[9] == '-') {fill_box(9, 'O'); return true;}
    else if(box[1] == 'X' && box[9] == 'X' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'X' && box[9] == 'X' && box[1] == '-') {fill_box(1, 'O'); return true;}
 
    else if(box[3] == 'X' && box[5] == 'X' && box[7] == '-') {fill_box(7, 'O'); return true;}
    else if(box[3] == 'X' && box[7] == 'X' && box[5] == '-') {fill_box(5, 'O'); return true;}
    else if(box[5] == 'X' && box[7] == 'X' && box[3] == '-') {fill_box(3, 'O'); return true;}
 
    return false;
}
 
void computer_use_random(){
    if(rand_pos == 0){
        if(box[5]== '-')      {box[5] = 'O'; fill_box(5, 'O');}
        else if(box[1]== '-') {box[1] = 'O'; fill_box(1, 'O');}
        else if(box[9]== '-') {box[9] = 'O'; fill_box(9, 'O');}
        else if(box[7]== '-') {box[7] = 'O'; fill_box(7, 'O');}
        else if(box[2]== '-') {box[2] = 'O'; fill_box(2, 'O');}
        else if(box[6]== '-') {box[6] = 'O'; fill_box(6, 'O');}
        else if(box[3]== '-') {box[3] = 'O'; fill_box(3, 'O');}
        else if(box[8]== '-') {box[8] = 'O'; fill_box(8, 'O');}
        else if(box[4]== '-') {box[4] = 'O'; fill_box(4, 'O');}
    }else if(rand_pos == 1){
        if(box[5]== '-')      {box[5] = 'O'; fill_box(5, 'O');}
        else if(box[1]== '-') {box[1] = 'O'; fill_box(1, 'O');}
        else if(box[9]== '-') {box[9] = 'O'; fill_box(9, 'O');}
        else if(box[8]== '-') {box[8] = 'O'; fill_box(8, 'O');}
        else if(box[2]== '-') {box[2] = 'O'; fill_box(2, 'O');}
        else if(box[6]== '-') {box[6] = 'O'; fill_box(6, 'O');}
        else if(box[3]== '-') {box[3] = 'O'; fill_box(3, 'O');}
        else if(box[7]== '-') {box[7] = 'O'; fill_box(7, 'O');}
        else if(box[4]== '-') {box[4] = 'O'; fill_box(4, 'O');}
    }else if(rand_pos == 2){
        if(box[5]== '-')      {box[5] = 'O'; fill_box(5, 'O');}
        else if(box[1]== '-') {box[1] = 'O'; fill_box(1, 'O');}
        else if(box[9]== '-') {box[9] = 'O'; fill_box(9, 'O');}
        else if(box[6]== '-') {box[6] = 'O'; fill_box(6, 'O');}
        else if(box[2]== '-') {box[2] = 'O'; fill_box(2, 'O');}
        else if(box[8]== '-') {box[8] = 'O'; fill_box(8, 'O');}
        else if(box[3]== '-') {box[3] = 'O'; fill_box(3, 'O');}
        else if(box[7]== '-') {box[7] = 'O'; fill_box(7, 'O');}
        else if(box[4]== '-') {box[4] = 'O'; fill_box(4, 'O');}
    }
}
 
bool is_user_wins(){
    if(box[1] == 'X' && box[2] == 'X' && box[3] == 'X'){
        return true;
    }else if(box[1] == 'X' && box[4] == 'X' && box[7] == 'X'){
        return true;
    }else if(box[1] == 'X' && box[5] == 'X' && box[9] == 'X'){
        return true;
    }else if(box[2] == 'X' && box[5] == 'X' && box[8] == 'X'){
        return true;
    }else if(box[3] == 'X' && box[5] == 'X' && box[7] == 'X'){
        return true;
    }else if(box[3] == 'X' && box[6] == 'X' && box[9] == 'X'){
        return true;
    }else if(box[4] == 'X' && box[5] == 'X' && box[6] == 'X'){
        return true;
    }else if(box[7] == 'X' && box[8] == 'X' && box[9] == 'X'){
        return true;
    }
 
    return false;
}
 
bool is_computer_wins(){
    if(box[1] == 'O' && box[2] == 'O' && box[3] == 'O'){
        return true;
    }else if(box[1] == 'O' && box[4] == 'O' && box[7] == 'O'){
        return true;
    }else if(box[1] == 'O' && box[5] == 'O' && box[9] == 'O'){
        return true;
    }else if(box[2] == 'O' && box[5] == 'O' && box[8] == 'O'){
        return true;
    }else if(box[3] == 'O' && box[5] == 'O' && box[7] == 'O'){
        return true;
    }else if(box[3] == 'O' && box[6] == 'O' && box[9] == 'O'){
        return true;
    }else if(box[4] == 'O' && box[5] == 'O' && box[6] == 'O'){
        return true;
    }else if(box[7] == 'O' && box[8] == 'O' && box[9] == 'O'){
        return true;
    }
 
    return false;
}
 
bool is_draw(){
    if(box[1] != '-' && box[2] != '-' && box[3] != '-' && box[4] != '-' && box[5] != '-' && box[6] != '-' && box[7] != '-' && box[8] != '-' && box[9] != '-'){
        return true;
    }else{
        return false;
    }
}
 
void user_wins(){
    cout << endl << "Anda menang! Selamat!" << endl;
    game_over = true;
}
 
void computer_wins(){
    cout << endl << "Anda kalah! ulangi lagi." << endl;
    game_over = true;
}
 
void draw(){
    cout << endl << "Permainan draw! ulangi lagi." << endl;
    game_over = true;
}

Lihat semua daftar ACS - Download: tictactoe.cpp - Tanggal: 5 Mei 2013 - Kategori: C++