マルバツゲーム
Posted feedbacks - C++
○×ゲームなのに視覚的なものはほとんど考慮してないので恐縮ですが... ちなみに、100万回勝負させたら player 1 won : 3595448(0.359545) player 2 won : 2882753(0.288275) draw : 3521799(0.35218) だそうです。
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 | #include <algorithm>
#include <iostream>
#include <map>
#include <ctime>
class board
{
int cell[3*3];
public:
board() { reset(); }
void reset() { std::fill_n(cell, 3*3, 0); }
int& operator()(int x, int y)
{ return cell[x+3*y]; }
const int& operator()(int x, int y) const
{ return cell[x+3*y]; }
};
std::ostream& operator<<(std::ostream& os, const board& b)
{
for ( int y = 0; y < 3; ++y ) {
y != 0 && os << "-+-+-\n";
os << b(0,y) << "|" << b(1,y) << "|" << b(2,y) << "\n";
}
return os;
}
class player
{
static int player_id_;
int id_;
protected:
player() : id_(++player_id_) {}
virtual ~player();
public:
int id()const { return id_; }
void operator()(board& b) const
{ next(b); }
private:
virtual void
next(board& b)
const = 0;
};
int player::player_id_ = 0;
player::~player() {}
class rand_player
: public player
{
void next(board& b) const;
};
void
rand_player::next(board& b)
const
{
while (1) {
int x = std::rand() % 3;
int y = std::rand() % 3;
if ( b(x,y) == 0 ) {
b(x,y) = id();
break;
}
}
}
struct board_checker
{
int operator()(board& b) const;
};
int
board_checker::operator()(board& b)
const
{
bool draw=true;
for (int x = 0; draw && x < 3; ++x )
for (int y = 0; draw && y < 3; ++y )
draw = b(x,y) != 0;
if ( draw )
return -1;
if ( int id = b(1,1) )
if ((b(0,0) == id && b(2,2) == id ) ||
(b(0,2) == id && b(2,0) == id) ||
(b(0,1) == id && b(2,1) == id) ||
(b(1,0) == id && b(1,2) == id) )
return id;
if ( int id = b(0,0) )
if ((b(0,1) == id && b(0,2) == id) ||
(b(1,0) == id && b(2,0) == id) )
return id;
if ( int id = b(2,2) )
if ((b(1,2) == id && b(0,2) == id) ||
(b(2,1) == id && b(2,0) == id) )
return id;
// not finish
return 0;
}
int main()
{
std::srand(std::time(0));
rand_player p1, p2;
player *p[] = { &p1, &p2 };
board b;
board_checker bc;
std::map<int, unsigned int> st;
const std::size_t max = 10000
for ( std::size_t i = 0; i < max; ++i ) {
b.reset();
int count = 0;
while ( 1 ) {
(*p[count++ % 2])(b);
// std::cout << " i = " << i << " step = " << count << "\n" << b;
if ( count >= 4 )
if ( int result = bc(b) ) {
st[result]++;
break;
}
}
}
std::cout << "player " << p1.id() << " won : " << st[p1.id()]
<< "(" << st[p1.id()]/static_cast<double>(max) << ")\n";
std::cout << "player " << p2.id() << " won : " << st[p2.id()]
<< "(" << st[p2.id()]/static_cast<double>(max) << ")\n";
std::cout << "draw : " << st[-1]
<< "(" << st[-1]/static_cast<double>(max) << ")\n";
return 0;
}
|
こんにちは。 お手軽実装のはずが、コンパイラにおこられて、ソース分割する羽目になりました。 無駄に拡張性を重視したもので、長くなり、冗長になりました。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,
};
//////////////////////////////////////////////////////////////
|
作ったので投稿してみます 実行結果 Player1 : 585272 Player2 : 288196 Draw : 126532
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | #include <iostream>
#include <ctime>
//#define DEBUG
const int CLEAR_LINE = 3;
const int HEIGHT = 3;
const int WIDTH = 3;
const int TEST = 1000000;
enum FIELD_STATE
{
NON_PLAYER,
PLAYER1,
PLAYER2
};
enum RESULT
{
NEXT,
PLAYER1_WIN,
PLAYER2_WIN,
DRAW
};
class Field
{
private:
FIELD_STATE m_field[WIDTH][HEIGHT];
public:
Field() { }
~Field() { }
void init()
{
for(int i=0; i<WIDTH; i++)
{
for(int j=0; j<HEIGHT; j++)
{
m_field[i][j] = NON_PLAYER;
}
}
}
void print()
{
for(int i=0; i<HEIGHT; i++)
{
for(int j=0; j<WIDTH; j++)
{
switch(m_field[i][j])
{
case PLAYER1: std::cout << "○" << std::flush; break;
case PLAYER2: std::cout << "×" << std::flush; break;
default : std::cout << " " << std::flush; break;
}
if(j != WIDTH-1) std::cout << "|" << std::flush;
}
if(i != HEIGHT-1) std::cout << "\n----------" << std::endl;
}
}
void setField(int x, int y, FIELD_STATE in_PlayerType)
{
m_field[x][y] = in_PlayerType;
}
FIELD_STATE getField(int in_height, int in_width)
{
return m_field[in_height][in_width];
}
};
class Player
{
private:
FIELD_STATE m_player;
public:
Player() { }
virtual ~Player() { }
Player(FIELD_STATE in_player)
{
m_player = in_player;
}
void setPlayer(FIELD_STATE in_player)
{
m_player = in_player;
}
FIELD_STATE getPlayer()
{
return m_player;
}
virtual void turn(Field* in_field)
{
int x;
int y;
while(true)
{
x = rand() % WIDTH;
y = rand() % HEIGHT;
if(in_field->getField(x, y) == NON_PLAYER)
{
in_field->setField(x, y, m_player);
return;
}
}
}
};
class Judg
{
public:
Judg() { }
virtual ~Judg() { }
RESULT check(Field* in_field)
{
int player1 = 0;
int player2 = 0;
// 横
for(int i=0; i<WIDTH; i++)
{
player1 = 0;
player2 = 0;
for(int j=0; j<HEIGHT; j++)
{
switch(in_field->getField(i, j))
{
case NON_PLAYER: break;
case PLAYER1: player1++; break;
case PLAYER2: player2++; break;
}
if(player1+player2 != j+1) break;
}
if(player1 == CLEAR_LINE) return PLAYER1_WIN;
if(player2 == CLEAR_LINE) return PLAYER2_WIN;
}
// 縦
for(int i=0; i<HEIGHT; i++)
{
player1 = 0;
player2 = 0;
for(int j=0; j<WIDTH; j++)
{
switch(in_field->getField(j, i))
{
case NON_PLAYER: break;
case PLAYER1: player1++; break;
case PLAYER2: player2++; break;
}
if(player1+player2 != j+1) break;
}
if(player1 == CLEAR_LINE) return PLAYER1_WIN;
if(player2 == CLEAR_LINE) return PLAYER2_WIN;
}
// 斜め
if(WIDTH != HEIGHT) return NEXT;
player1 = 0;
player2 = 0;
for(int i=0, j=0; i<WIDTH && j<HEIGHT; i++, j++)
{
switch(in_field->getField(i, j))
{
case NON_PLAYER: break;
case PLAYER1: player1++; break;
case PLAYER2: player2++; break;
}
if(player1+player2 != j+1) break;
else if(player1 == CLEAR_LINE) return PLAYER1_WIN;
else if(player2 == CLEAR_LINE) return PLAYER2_WIN;
}
player1 = 0;
player2 = 0;
for(int i=WIDTH-1, j=0; i>=0 && j<HEIGHT; i--, j++)
{
switch(in_field->getField(i, j))
{
case NON_PLAYER: break;
case PLAYER1: player1++; break;
case PLAYER2: player2++; break;
}
if(player1+player2 != j+1) break;
else if(player1 == CLEAR_LINE) return PLAYER1_WIN;
else if(player2 == CLEAR_LINE) return PLAYER2_WIN;
}
for(int i=0; i<WIDTH; i++)
{
for(int j=0; j<HEIGHT; j++)
{
if(in_field->getField(i, j) == NON_PLAYER)
{
return NEXT;
}
}
}
return DRAW;
}
};
int main()
{
Field field;
Judg judg;
RESULT state;
Player player[2];
int winner[3] = {0, 0, 0};
int i;
bool game;
srand( static_cast<unsigned int>(time(NULL)) );
player[0].setPlayer(PLAYER1);
player[1].setPlayer(PLAYER2);
for(int s=0; s<TEST; s++)
{
i = 0;
game = true;
field.init();
while(game)
{
player[i].turn(&field);
#ifdef DEBUG
std::cout << std::endl;
field.print();
std::cout << std::endl;
#endif
state = judg.check(&field);
switch(state)
{
case NEXT: break;
case PLAYER1_WIN:
case PLAYER2_WIN:
#ifdef DEBUG
std::cout << "Player " << (i+1) << " Won" << std::endl;
#endif
winner[i]++;
game = false;
break;
case DRAW:
#ifdef DEBUG
std::cout << "Draw Game" << std::endl;
#endif
winner[2]++;
game = false;
break;
}
i = i ^ 0x00000001;
}
}
std::cout << "Result" << std::endl;
std::cout << "Player1 :" << winner[0] << std::endl;
std::cout << "Player2 :" << winner[1] << std::endl;
std::cout << "Draw :" << winner[2] << std::endl;
return EXIT_SUCCESS;
}
|


syat
#6190()
Rating4/4=1.00
マルバツゲームは3×3の格子に交互に○と×を書き込み、先に縦・横・斜めに記号をそろえたほうが勝ちというおなじみのゲームです。
「毎ターン乱数を使って手を決めるランダムプレイヤー同士を対戦させる」というのが今回のお題です。 1万回対戦させ、勝ち・負け・引き分けの数を表示してください。 そして先手が有利であることを確かめてください。
良い手を思考するプレイヤーについては別のお題にしようと思っています。 プレイヤーを簡単に差し換えることができる設計を目指してください。
[ reply ]