LoginSignup
0
0

More than 5 years have passed since last update.

Tick-Tack-Toe

Last updated at Posted at 2015-02-25
Source.cpp
#include "Header.h"

TTT::TTT()
{
    for(int i=0; i<fild+1; i++)
        m_tttFild[i] = '*';
}

bool TTT::isWin(const char &mark)
{
    if((m_tttFild[1] == mark) && (m_tttFild[4] == mark) && (m_tttFild[7] == mark))
        return true;
    if((m_tttFild[2] == mark) && (m_tttFild[5] == mark) && (m_tttFild[8] == mark))
        return true;
    if((m_tttFild[3] == mark) && (m_tttFild[6] == mark) && (m_tttFild[9] == mark))
        return true;
    if((m_tttFild[1] == mark) && (m_tttFild[2] == mark) && (m_tttFild[3] == mark))
        return true;
    if((m_tttFild[4] == mark) && (m_tttFild[5] == mark) && (m_tttFild[6] == mark))
        return true;
    if((m_tttFild[7] == mark) && (m_tttFild[8] == mark) && (m_tttFild[9] == mark))
        return true;
    if((m_tttFild[1] == mark) && (m_tttFild[5] == mark) && (m_tttFild[9] == mark))
        return true;
    if((m_tttFild[3] == mark) && (m_tttFild[5] == mark) && (m_tttFild[7] == mark))
        return true;
    return false;
}

bool TTT::putMark(const int &putPlace, const char &mark)
{
    if(m_tttFild[putPlace] == '*'){
        m_tttFild[putPlace] = mark;
        return true;
    }
        return false;
}

const std::string TTT::solve(const std::string& input)
{
    for(int i=0; i<input.size() && i<fild; i++){
        if(i%2==0){
            if(!putMark((int)(input.at(i) -'0'), 'o')){
                return "Foul : x won.";
            }
            if(isWin('o')){
                return "o won.";
            }
        }
        if(i%2==1){
            if(!putMark((int)(input.at(i) -'0'), 'x')){
                return "Foul : o won.";
            }
            if(isWin('x')){
                return "x won.";
            }
        }
    }
    return "Draw game.";
}

int main()
{
    const std::string input = "35497162193";
    TTT ttt;
    std::cout << ttt.solve(input) << std::endl;
    return 0;
}
Source.h
#ifndef _HEADER_H
#define _HEADER_H

#include <string>
#include <iostream>

#define fild 9

class TTT
{
public:
    TTT();
    const std::string   solve(const std::string& input);
private:
    char m_tttFild[fild+1];
    bool isWin(const char &mark);
    bool putMark(const int &putPlace, const char &mark);
};

#endif
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0