練習用です。いろんなものがごちゃまぜです。
Revision | 09edd29220b41e9bbc97d1fbe03c8e7cfc1c2b95 (tree) |
---|---|
Time | 2017-03-08 16:48:35 |
Author | ![]() |
Commiter | bellyoshi |
テーブル駆動型に
@@ -1,48 +1,61 @@ | ||
1 | 1 | #include <iostream> |
2 | 2 | #include <string> |
3 | +#include <cassert> | |
3 | 4 | |
4 | 5 | using namespace std; |
5 | 6 | |
6 | 7 | const int DICE_SIZE = 6; |
7 | 8 | |
9 | +//ダイス | |
10 | +//東西南北に転がすことができる。 | |
8 | 11 | class dice { |
12 | + //一番上,南面,東面,西面,北面,底面の数字 | |
9 | 13 | int values[DICE_SIZE]; |
10 | 14 | public: |
11 | - dice (){}; | |
12 | - virtual ~dice (){}; | |
13 | - void west(); | |
14 | - void north(); | |
15 | - void east(); | |
16 | - void south(); | |
15 | + dice (); | |
16 | + virtual ~dice (); | |
17 | 17 | int &operator[](int i){ |
18 | - int idx = i % DICE_SIZE; | |
19 | - return values[idx]; | |
18 | + assert(0 <= i && i < DICE_SIZE); | |
19 | + return values[i]; | |
20 | 20 | }; |
21 | 21 | void docmd(string str); |
22 | 22 | void docmd(char c); |
23 | 23 | private: |
24 | - int westindex[4] = {0,2,5,3}; | |
25 | - int northindex[4] = {0,1,5,4}; | |
24 | + int indexs[4][4]; | |
25 | + char *cmdChar; | |
26 | 26 | void move_forward(int *indexs); |
27 | - void move_reverse(int *indexs); | |
28 | 27 | }; |
28 | +dice::dice(){ | |
29 | + int westindex[4] = {1, 3, 6, 4}; | |
30 | + int northindex[4] = {1, 2, 6, 5}; | |
31 | + cmdChar = new char[4]{'W', 'E', 'N', 'S'}; | |
32 | + for(int i = 0; i < 4; i++){ | |
33 | + indexs[0][i] = westindex[i] - 1;//W | |
34 | + indexs[1][i] = westindex[4 - i - 1] - 1;//E | |
35 | + indexs[2][i] = northindex[i] - 1;//N | |
36 | + indexs[3][i] = northindex[4 - i - 1] - 1;//S | |
37 | + } | |
38 | +} | |
39 | +dice::~dice(){ | |
40 | + delete [] cmdChar; | |
41 | +} | |
29 | 42 | |
30 | 43 | void dice::docmd(string str){ |
31 | 44 | for(int i = 0; i < str.length(); i++){ |
45 | + // cout << "docmd(str[i])" << str[i] << endl; | |
32 | 46 | docmd(str[i]); |
33 | 47 | } |
34 | 48 | } |
35 | 49 | |
36 | 50 | void dice::docmd(char c){ |
37 | - if(c == 'W'){ | |
38 | - west(); | |
39 | - } else if (c == 'E'){ | |
40 | - east(); | |
41 | - } else if (c == 'N'){ | |
42 | - north(); | |
43 | - } else if (c == 'S'){ | |
44 | - south(); | |
51 | + for(int i = 0; i < 4; i++){ | |
52 | + if (c == cmdChar[i]){ | |
53 | + // cout << cmdChar[i] << endl; | |
54 | + move_forward(indexs[i]); | |
55 | + return; | |
56 | + } | |
45 | 57 | } |
58 | + assert(false);//命令表にない命令 | |
46 | 59 | } |
47 | 60 | void dice::move_forward(int *indexs){ |
48 | 61 | int temp = values[indexs[0]]; |
@@ -51,27 +64,6 @@ void dice::move_forward(int *indexs){ | ||
51 | 64 | values[indexs[2]] = values[indexs[3]]; |
52 | 65 | values[indexs[3]] = temp; |
53 | 66 | } |
54 | -void dice::move_reverse(int *indexs){ | |
55 | - int temp = this->values[indexs[3]]; | |
56 | - values[indexs[3]] = values[indexs[2]]; | |
57 | - values[indexs[2]] = values[indexs[1]]; | |
58 | - values[indexs[1]] = values[indexs[0]]; | |
59 | - values[indexs[0]] = temp; | |
60 | -} | |
61 | -void dice::west(){ | |
62 | - move_forward(westindex); | |
63 | -} | |
64 | -void dice::east(){ | |
65 | - move_reverse(westindex); | |
66 | -} | |
67 | -void dice::north(){ | |
68 | - move_forward(northindex); | |
69 | -} | |
70 | -void dice::south(){ | |
71 | - move_reverse(northindex); | |
72 | -} | |
73 | - | |
74 | - | |
75 | 67 | |
76 | 68 | int main(int argc, char const *argv[]) { |
77 | 69 | dice d; |