• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

練習用です。いろんなものがごちゃまぜです。


Commit MetaInfo

Revisiona8973751e587fe54189315f17843989eee2dce1e (tree)
Time2017-04-09 11:17:15
Author渡邉 喜光 <tccwas@gmai...>
Commiter渡邉 喜光

Log Message

コンソールで人対人ができるように。

Change Summary

Incremental Difference

--- a/Reversi/Reversi2/ConsoleApplication1/ConsoleApplication1.cpp
+++ b/Reversi/Reversi2/ConsoleApplication1/ConsoleApplication1.cpp
@@ -3,12 +3,16 @@
33
44 #include "stdafx.h"
55 #include "../Reversi2/Board.h"
6+#include "../Reversi2/Move.h"
7+
68 #include <iostream>
79 using namespace std;
810
911 void output(Board &board) {
10- for (int y = 0; y < ROW_MEM_SIZE; y++) {
11- for (int x = 0; x < COL_MEM_SIZE; x++) {
12+ cout << " 123456789\n";
13+ for (int y = 1; y <= ROW_SIZE; y++) {
14+ cout << y;
15+ for (int x = 1; x <= COL_SIZE; x++) {
1216 int c = board.Cells(x, y);
1317 if (c == BLACK) {
1418 cout << "●";
@@ -29,10 +33,26 @@ void output(Board &board) {
2933 cout << endl;
3034 }
3135 }
36+void input(Move &move) {
37+ int x, y;
38+ cin >> x >> y;
39+ move.drop_pos = x + y * COL_MEM_SIZE;
40+}
3241 int main()
3342 {
3443 Board b;
3544 output(b);
45+ while (1) {
46+ Move move;
47+ input(move);
48+
49+ b.AddRevPosToMove(move);
50+ if (move.rev_pos_count == 0) {
51+ continue;
52+ }
53+ b.Do(&move);
54+ output(b);
55+ }
3656 return 0;
3757 }
3858
--- a/Reversi/Reversi2/Reversi2/Board.cpp
+++ b/Reversi/Reversi2/Reversi2/Board.cpp
@@ -1,10 +1,16 @@
11 #include "stdafx.h"
22 #include "Board.h"
33
4+#define countof(x) sizeof(x) / sizeof(x[0])
45
56 void Board::Do(Move *move)
67 {
78 moves.push_back(*move);
9+ cells[move->drop_pos] = color_of_turn;
10+ for (int i = 0; i < move->rev_pos_count; i++) {
11+ cells[move->rev_pos[i]] = color_of_turn;
12+ }
13+ color_of_turn = -color_of_turn;
814 }
915
1016 void Board::Undo()
@@ -12,6 +18,56 @@ void Board::Undo()
1218 moves.pop_back();
1319 }
1420
21+
22+
23+void Board::AddRevPosToMove(Move & move)
24+{
25+ const int UP = -COL_MEM_SIZE;
26+ const int DOWN = COL_MEM_SIZE;
27+ const int LEFT = -1;
28+ const int RIGHT = 1;
29+
30+ int directions[8] = {
31+ UP,
32+ DOWN,
33+ LEFT,
34+ RIGHT,
35+ UP + RIGHT,
36+ UP + LEFT,
37+ DOWN + RIGHT ,
38+ DOWN + LEFT };
39+
40+ //マスが空でなければ打てない。
41+ if (cells[move.DropPosition()] != EMPTY) {
42+ return;
43+ }
44+
45+ //8方向石が挟まれているかを確認
46+ for (int i = 0; i < countof(directions); i++) {
47+
48+ //現在の方向がひっくり返せない場合は元に戻すためにひっくり返すのが確定した
49+ //石の数を覚えておく。
50+ int pre_rev_pos_count = move.rev_pos_count;
51+
52+ //まずうった石の隣のマスに注目する。
53+ int pos = move.DropPosition();
54+ pos += directions[i];
55+
56+ while (cells[pos] == -color_of_turn) {
57+ //反対の色の石が続いていれば仮にひっくり返す位置に追加する。
58+ move.AddRevpos(pos);
59+ //隣の石に移動
60+ pos += directions[i];
61+ //最後が壁か空ということは同じ色の石で挟まれていない。
62+ if (cells[pos] == WALL || cells[pos] == EMPTY) {
63+ //仮にひっくり返る石に追加したものをキャンセルする。
64+ move.rev_pos_count = pre_rev_pos_count;
65+ break;
66+ }
67+ }
68+ }
69+}
70+
1571 Board::Board()
1672 {
1773 //四方を壁にする。
@@ -45,7 +101,7 @@ Board::Board()
45101
46102 //初手にする。
47103 turn_count = 0;
48-
104+ color_of_turn = BLACK;
49105
50106 }
51107
--- a/Reversi/Reversi2/Reversi2/Board.h
+++ b/Reversi/Reversi2/Reversi2/Board.h
@@ -28,6 +28,9 @@ const int WHITE = -1;
2828 const int EMPTY = 0;
2929 const int WALL = 2;
3030
31+/// <summary>
32+///
33+/// </summary>
3134 class Board
3235 {
3336 /// <summary>
@@ -80,6 +83,15 @@ public:
8083
8184 void Undo();
8285
86+
87+
88+ /// <summary>
89+ /// Adds the rev position to move.
90+ /// 置くことができなければMove.ReverseCount == 0
91+ /// </summary>
92+ /// <param name="move">The move.</param>
93+ void AddRevPosToMove(Move &move);
94+
8395 Board();
8496 ~Board();
8597 };
--- a/Reversi/Reversi2/Reversi2/Move.cpp
+++ b/Reversi/Reversi2/Reversi2/Move.cpp
@@ -4,6 +4,7 @@
44
55 Move::Move()
66 {
7+ rev_pos_count = 0;
78 }
89
910
--- a/Reversi/Reversi2/Reversi2/Move.h
+++ b/Reversi/Reversi2/Reversi2/Move.h
@@ -5,14 +5,16 @@
55 /// <summary>
66 /// 差し手
77 /// </summary>
8-class Move
8+struct Move
99 {
10-
1110 /// <summary>
1211 /// The drop down position
1312 /// 石を置いた場所
1413 /// </summary>
1514 int drop_pos;
15+
16+ int rev_pos_count;
17+
1618 /// <summary>
1719 /// The reverse positions
1820 /// ひっくり返る位置
@@ -20,13 +22,30 @@ class Move
2022 /// それは角に置いたときで3方向×6個 = 18個
2123 /// </summary>
2224 int rev_pos[18];
23- int rev_pos_count;
24-public:
25+
26+ //void RevPosCommit(void) {
27+ // pre_rev_pos_count = rev_pos_count;
28+ //}
29+
30+ //void RevPosAbort(void) {
31+ // rev_pos_count = pre_rev_pos_count;
32+ //}
2533
2634 void AddRevpos(int position) {
2735 rev_pos[rev_pos_count] = position;
2836 rev_pos_count++;
2937 }
38+ Move(int position) {
39+ drop_pos = position;
40+ rev_pos_count = 0;
41+ }
42+ int ReverseCount() {
43+ return rev_pos_count;
44+ }
45+ int DropPosition() {
46+ return drop_pos;
47+ }
48+
3049 Move();
3150 ~Move();
3251 };