Comment detail

マルバツゲーム (Nested Flatten)

こんにちは。 お手軽実装のはずが、コンパイラにおこられて、ソース分割する羽目になりました。 無駄に拡張性を重視したもので、長くなり、冗長になりました。orz でもその代わりに、長さ3以外もいけます。 あぁ、バグ取りはしましたけど、保障はしません。

result: Player1[5853] Player2[2865] Draw[1282]

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//////////////////////Main.cpp////////////////////////////
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

#include "MBGame.h"
#include "Player.h"

void ShowVisual(MBGame& in){
    Mark V;
        for(unsigned int i=0;i<in.Length();i++){
            for(unsigned int j=0;j<in.Length();j++){
                V=in.At(j,i);
                if(V == None) printf("+");
                if(V == Maru) printf("O");
                if(V == Batu) printf("X");
            }
            printf("\n");
        }
}

int main(){
    unsigned int Len = 3;
    MBGame mbg(Len);
    Player A,B;
    Mark F;
    int P1=0,P2=0,D=0;
    //srand(time(NULL));
    mbg.SetPlayer(A,B);
    for(int c=0;c<100;c++){
        mbg.Init(Len);
        F = mbg.Play();
        if(F == None) {printf("[%d] Draw!!\n",c);D++;}
        if(F == Maru) {printf("[%d] O Win!\n",c);P1++;}
        if(F == Batu) {printf("[%d] X Win!\n",c);P2++;}
        ShowVisual(mbg);
        printf("\n");
    }
    printf("Player1[%d] Player2[%d] Draw[%d]",P1,P2,D);
    return 0;
}
///////////////////////////MBGame.h////////////////////
#pragma once
#include <vector>

#include "Mark.h"
#include "Player.h"
class Player;
class MBGame{
    Player *A_,*B_;
    unsigned int Length_;
    std::vector<Mark> Board_;
    bool IsLock_;

public:
    MBGame(unsigned int Len=3);
    void Init(unsigned int Len=3);
    unsigned int Length();

    Mark Play();
    Mark IsEnd();
    bool IsEmpty(unsigned int x,unsigned int y);
    bool HasEmpty();
    bool SetStone(unsigned int x,unsigned int y,Mark M);
    bool IsLocked();
    Mark At(unsigned int x,unsigned int y);
    void SetPlayer(Player& A,Player& B);
protected:
    void Lock();
    void UnLock();
};
///////////////////////////////MBGame.cpp/////////////
#include <algorithm>
#include "MBGame.h"

MBGame::MBGame(unsigned int Len){
    A_=NULL;
    B_=NULL;
    Init(Len);
}

void MBGame::Init(unsigned int Len){
    if(Len == 0) Len = 3;
    Length_ = Len;
    Board_.resize(Len*Len);
    std::fill(Board_.begin(),Board_.end(),None);
}
bool MBGame::SetStone(unsigned int x,unsigned int y,Mark M){
    if(IsLocked()) return false;
    if(!IsEmpty(x,y)) return false;
    Board_[x+ (y*Length())] = M;
    Lock();
    return true;
}
void MBGame::SetPlayer(Player& A,Player& B){
    A_ = &A;
    B_ = &B;
    A_->SetMark(Maru);
    B_->SetMark(Batu);
}
/////////////////////////////////////////////////////////////////
Mark MBGame::Play(){
    Mark f;
    unsigned int c=0;
    while(HasEmpty()){
        if((c%2) == 0 ){
            UnLock();
            A_->Play(*this);
        }else{
            UnLock();
            B_->Play(*this);
        }
        f=IsEnd();
        if(f!=None) return f;
        c++;
    }
    return None;
}

Mark MBGame::IsEnd(){
    Mark F=None;
    unsigned int i=0,j=0;
    for(i=0;i<Length();i++){//横の判定
        F = At(0,i);
        if(F == None) continue;
        for(j=0;j<Length();j++){
            if(F != At(j,i)) break;
        }
        if(j==Length()) return F;
    }
    
    for(j=0;j<Length();j++){//縦の判定
        F = At(j,0);
        if(F == None) continue;
        for(i=0;i<Length();i++){
            if(F != At(j,i)) break;
        }
        if(i==Length()) return F;
    }
    
    F = At(0,0);
    for(i=0;i<Length();i++){//斜めの判定
        if(F == None) break;
        if(F != At(i,i)) break;
    }
    if(i == Length()) return F;

    F = At((Length()-1),0);
    for(i=0;i<Length() ;i++){//斜めの判定2
        if(F == None) break;
        if(F != At((Length()-1) - i,i)) break;
    }
    if(i == Length()) return F;
    return None;
}
/////////////////////////////////////////////////////////////////
bool MBGame::IsEmpty(unsigned int x,unsigned int y){
    return At(x,y) == None;
}

bool MBGame::HasEmpty(){
    for(unsigned int i=0;i<Length();i++){
        for(unsigned int j=0;j<Length();j++){
            if(IsEmpty(j,i)) return true;
        }
    }
    return false;
}
/////////////////////////////////////////////////////////////////
Mark MBGame::At(unsigned int x,unsigned int y){
    return Board_[y*Length() + x];
}
unsigned int MBGame::Length(){
    return Length_;
}
/////////////////////////////////////////////////////////////////
bool MBGame::IsLocked(){
    return IsLock_;
}
void MBGame::Lock(){
    IsLock_ = true;
}
void MBGame::UnLock(){
    IsLock_ = false;
}
////////////////////////////////////////////////////////////////

/////////////////////////Player.h//////////////////////////
#pragma once;
#include "MBGame.h"

class MBGame;
class Player{
    Mark Mark_;
public:
    void SetMark(Mark in);
    virtual void Play(MBGame& in);
};
////////////////////////Player.cpp////////////////////////
#include "Player.h"
void Player::SetMark(Mark in){
    Mark_ = in;
}
void Player::Play(MBGame &in){
        unsigned int x,y;
        do{
            x=(rand()%in.Length());
            y=(rand()%in.Length());
            //if(!in.IsEmpty(x,y)) continue;
        }while(!in.SetStone(x,y,Mark_));
}
////////////////////////////Mark.h//////////////////////////
#pragma once;
enum Mark{
        Batu =2,
        None =0,
        Maru =1,
};
//////////////////////////////////////////////////////////////

Index

Feed

Other

Link

Pathtraq

loading...