練習用です。いろんなものがごちゃまぜです。
Revision | f73fb7d99516c50c304cd78eca6a7d32c955384a (tree) |
---|---|
Time | 2017-03-09 09:07:25 |
Author | ![]() |
Commiter | bellyoshi |
dice 2
@@ -0,0 +1,118 @@ | ||
1 | +#include <iostream> | |
2 | +#include <string> | |
3 | +#include <cassert> | |
4 | + | |
5 | +using namespace std; | |
6 | + | |
7 | +const int DICE_SIZE = 6; | |
8 | +int const CmdCount = 6; | |
9 | +int const RotateLen = 4; | |
10 | + | |
11 | +//ダイス | |
12 | +//東西南北に転がすことができる。 | |
13 | +class dice { | |
14 | + //一番上,南面,東面,西面,北面,底面の数字 | |
15 | + int values[DICE_SIZE]; | |
16 | +public: | |
17 | + dice (); | |
18 | + virtual ~dice (); | |
19 | + int &operator[](int i){ | |
20 | + assert(0 <= i && i < DICE_SIZE); | |
21 | + return values[i]; | |
22 | + }; | |
23 | + void docmd(string str); | |
24 | + void docmd(char c); | |
25 | + void setTopAndFront(int top,int front); | |
26 | + void input(void); | |
27 | + int right(void){return values[2];}; | |
28 | + int top(void){return values[0];}; | |
29 | + int front(void){return values[1];}; | |
30 | +private: | |
31 | + | |
32 | + int indexs[CmdCount][RotateLen]; | |
33 | + char *cmdChar; | |
34 | + string *toTopCmd; | |
35 | + string *toFrontCmd; | |
36 | + void move_forward(int *indexs); | |
37 | + int find_value(int num); | |
38 | +}; | |
39 | +void dice::setTopAndFront(int top, int front){ | |
40 | + int idxTop = find_value(top); | |
41 | + docmd(toTopCmd[idxTop]); | |
42 | + int idxFront = find_value(front); | |
43 | + docmd(toFrontCmd[idxFront]); | |
44 | +} | |
45 | +int dice::find_value(int num){ | |
46 | + for(int i = 0; i < DICE_SIZE; i++){ | |
47 | + if(values[i] == num){ | |
48 | + return i; | |
49 | + } | |
50 | + } | |
51 | + assert(false); | |
52 | +} | |
53 | +dice::dice(){ | |
54 | + int westindex[RotateLen] = {1, 3, 6, 4}; | |
55 | + int northindex[RotateLen] = {1, 2, 6, 5}; | |
56 | + int leftindex[RotateLen] = {4, 2, 3, 5}; | |
57 | + cmdChar = new char[CmdCount]{'W', 'E', 'N', 'S','L','R'}; | |
58 | + for(int i = 0; i < RotateLen; i++){ | |
59 | + indexs[0][i] = westindex[i] - 1;//W | |
60 | + indexs[1][i] = westindex[RotateLen - i - 1] - 1;//E | |
61 | + indexs[2][i] = northindex[i] - 1;//N | |
62 | + indexs[3][i] = northindex[RotateLen - i - 1] - 1;//S | |
63 | + indexs[4][i] = leftindex[i] - 1;//L | |
64 | + indexs[5][i] = leftindex[RotateLen - i - 1] - 1;//R | |
65 | + } | |
66 | + toTopCmd = new string[DICE_SIZE]{"","N","W","E","S","NN"}; | |
67 | + toFrontCmd = new string[DICE_SIZE]{"S","","L","R","LL","N"}; | |
68 | +} | |
69 | +dice::~dice(){ | |
70 | + delete [] cmdChar; | |
71 | + delete [] toTopCmd; | |
72 | + delete [] toFrontCmd; | |
73 | +} | |
74 | + | |
75 | +void dice::docmd(string str){ | |
76 | + for(int i = 0; i < str.length(); i++){ | |
77 | + // cout << "docmd(str[i])" << str[i] << endl; | |
78 | + docmd(str[i]); | |
79 | + } | |
80 | +} | |
81 | + | |
82 | +void dice::docmd(char c){ | |
83 | + for(int i = 0; i < CmdCount; i++){ | |
84 | + if (c == cmdChar[i]){ | |
85 | + // cout << cmdChar[i] << endl; | |
86 | + move_forward(indexs[i]); | |
87 | + return; | |
88 | + } | |
89 | + } | |
90 | + assert(false);//命令表にない命令 | |
91 | +} | |
92 | +void dice::move_forward(int *indexs){ | |
93 | + int temp = values[indexs[0]]; | |
94 | + values[indexs[0]] = values[indexs[1]]; | |
95 | + values[indexs[1]] = values[indexs[2]]; | |
96 | + values[indexs[2]] = values[indexs[3]]; | |
97 | + values[indexs[3]] = temp; | |
98 | +} | |
99 | +void dice::input(void){ | |
100 | + for(int i=0; i < DICE_SIZE; i++){ | |
101 | + int n; | |
102 | + cin >> n; | |
103 | + values[i] = n; | |
104 | + } | |
105 | +} | |
106 | +int main(int argc, char const *argv[]) { | |
107 | + dice d; | |
108 | + d.input(); | |
109 | + int count; | |
110 | + cin >> count; | |
111 | + for(int i = 0; i < count ;i++){ | |
112 | + int top,front; | |
113 | + cin >> top >> front; | |
114 | + d.setTopAndFront(top,front); | |
115 | + cout << "right:" << d.right() << "top:" << d.top() << "front:" << d.front() << endl; | |
116 | + } | |
117 | + return 0; | |
118 | +} |