• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

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


Commit MetaInfo

Revision09edd29220b41e9bbc97d1fbe03c8e7cfc1c2b95 (tree)
Time2017-03-08 16:48:35
Authorbellyoshi <tccwas@gmai...>
Commiterbellyoshi

Log Message

テーブル駆動型に

Change Summary

Incremental Difference

--- a/CPP/ITP1_11/a.cpp
+++ b/CPP/ITP1_11/a.cpp
@@ -1,48 +1,61 @@
11 #include <iostream>
22 #include <string>
3+#include <cassert>
34
45 using namespace std;
56
67 const int DICE_SIZE = 6;
78
9+//ダイス
10+//東西南北に転がすことができる。
811 class dice {
12+ //一番上,南面,東面,西面,北面,底面の数字
913 int values[DICE_SIZE];
1014 public:
11- dice (){};
12- virtual ~dice (){};
13- void west();
14- void north();
15- void east();
16- void south();
15+ dice ();
16+ virtual ~dice ();
1717 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];
2020 };
2121 void docmd(string str);
2222 void docmd(char c);
2323 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;
2626 void move_forward(int *indexs);
27- void move_reverse(int *indexs);
2827 };
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+}
2942
3043 void dice::docmd(string str){
3144 for(int i = 0; i < str.length(); i++){
45+ // cout << "docmd(str[i])" << str[i] << endl;
3246 docmd(str[i]);
3347 }
3448 }
3549
3650 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+ }
4557 }
58+ assert(false);//命令表にない命令
4659 }
4760 void dice::move_forward(int *indexs){
4861 int temp = values[indexs[0]];
@@ -51,27 +64,6 @@ void dice::move_forward(int *indexs){
5164 values[indexs[2]] = values[indexs[3]];
5265 values[indexs[3]] = temp;
5366 }
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-
7567
7668 int main(int argc, char const *argv[]) {
7769 dice d;