質問するログイン新規登録

質問編集履歴

5

コードのデバッグを掲載

2020/07/25 02:24

投稿

ryamamot0
ryamamot0

スコア3

title CHANGED
File without changes
body CHANGED
@@ -162,4 +162,6 @@
162
162
  }
163
163
  };
164
164
 
165
- ```
165
+ ```
166
+
167
+ ![コードのデバック](7323db551ce84119ce26ad82aee47859.png)

4

コードの修正

2020/07/25 02:24

投稿

ryamamot0
ryamamot0

スコア3

title CHANGED
File without changes
body CHANGED
@@ -28,10 +28,15 @@
28
28
  ```
29
29
  ```C++
30
30
  //MemoryMatchGame.h
31
+
31
32
  #include <iostream>
32
33
  #include <vector>
33
34
  #include <string>
35
+ #include <random>
36
+ #include <numeric>
37
+ #include <cassert>
34
38
  #include <time.h>
39
+ #include <iomanip>
35
40
  using namespace std;
36
41
 
37
42
  class MemoryMatchGame
@@ -83,6 +88,7 @@
83
88
  //ここでランダムに数値を入れ替えていく
84
89
  //後ろから順番に順に交換する.
85
90
  //ここでshuffleArrの中身はシャッフルされる
91
+ /*
86
92
  for (int i = 50; i > 1; --i)
87
93
  {
88
94
  int a = i-1;
@@ -92,6 +98,7 @@
92
98
  shuffleArr[a] = shuffleArr[b];
93
99
  shuffleArr[b] = tmp;
94
100
  }
101
+ */
95
102
  //tmpArrで1次元配列を作成する
96
103
  int tmpArr[randomNum];
97
104
  //tmpArrの中にランダムに作成した数値を入れる.
@@ -100,7 +107,36 @@
100
107
  tmpArr[i] = shuffleArr[i];
101
108
  };
102
109
  //cout << element[tmpArr[i]]でstring elementを呼び出す(後で書く)
110
+
111
+
112
+ assert( gridSize*gridSize % 2 == 0 );
113
+ int num = gridSize * gridSize / 2;
114
+ int* data = new int[num*2];
115
+ // 「0~num-1 で埋める」を二回やって
116
+ iota(data, data+num, 0);
117
+ iota(data+num, data+num*2, 0);
118
+ // かきまぜる
119
+ for (int i = 50; i > 1; --i)
120
+ {
121
+ int a = i-1;
122
+ int b = rand()%i;
123
+ //change shuffleArr[a] and ShuffleArr[b]
124
+ int tmp = shuffleArr[a];
125
+ shuffleArr[a] = shuffleArr[b];
126
+ shuffleArr[b] = tmp;
127
+ }
103
128
 
129
+
130
+ for (int r = 0; r < gridSize; ++r)
131
+ {
132
+ for (int c = 0; c < gridSize; ++c)
133
+ {
134
+ cout << gridArr[r][c] << ' ';
135
+ }
136
+ cout << endl;
137
+ }
138
+
139
+
104
140
  //gridを作成している
105
141
  for(int iii = 0; iii<gridSize;iii++)
106
142
  {
@@ -125,4 +161,5 @@
125
161
 
126
162
  }
127
163
  };
164
+
128
165
  ```

3

コードを変更

2020/07/25 02:22

投稿

ryamamot0
ryamamot0

スコア3

title CHANGED
File without changes
body CHANGED
@@ -27,6 +27,7 @@
27
27
  }
28
28
  ```
29
29
  ```C++
30
+ //MemoryMatchGame.h
30
31
  #include <iostream>
31
32
  #include <vector>
32
33
  #include <string>
@@ -39,36 +40,46 @@
39
40
  int gridSize;
40
41
  int theme;
41
42
  int timer;
42
- string element[50] = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn"};
43
+ string element[50] = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn"};
43
44
  string fruits[50] = {"grape","apple","tangerine","satsuma","strawberry","pear","apricot","lime","orange","cantaloupe","jujube","papaya","fig","banana","honeydew","cherry","mango","kiwi","tomato","huckleberry","boysenberry","nectarine","grapefruit","pomegranate","passon_fruit","coconut","clementine","date","starfruit","peach","plum","blackberry","kumquat","guava","dragonfruit","raspberry","watermelon","pineapple","blueberry","avocado","lemon"};
44
- string candy[50] = {"Almond Joy","Cotton candy","Atomic Fireball","Twizzlers","Nik-L-Nips","Jawbreakers","Red Vines","Snickers","Junior Mints","Werther's Original","Smarties,Gummi Worms","Pixie Stix","Gummi Bears","Tootsie Rolls","Blow Pops","Peeps","Tootsie Roll Pops","Hershey Bar","Sour Patch Kids","Necco wafers","Three Musketeers","York Peppermint Patties","Candy corn","Butterfinger","Baby Ruth","Kit-Kat","Pocky","Jelly Belly","Jolly Ranchers","Mr. Goodbar","Nerds","Sugar Daddy","Swedish Fish","M&Ms","Toblerone","Circus peanuts","Skittles","Pop Rocks","Reese's pieces","Candy cigarettes","Twix","Milky Way","Milk Duds","Wax lips","Hershey's Kisses","Cadbury eggs","Starburst","Payday","Reese's Peanut Butter Cups"}; void start(){
45
- cout << "グリッドサイズを洗濯して下さい:" << endl;
46
- cout << "4*4をプレイしたい場合は4を入力して下さい" << endl;
47
- cout << "6*6をプレイしたい場合は6を入力して下さい" << endl;
48
- cout << "8*8をプレイしたい場合は8を入力して下さい" << endl;
49
- cout << "選んでください: ";
50
- cin >> gridsize;
45
+ string candy[50] = {"Almond Joy","Cotton candy","Atomic Fireball","Twizzlers","Nik-L-Nips","Jawbreakers","Red Vines","Snickers","Junior Mints","Werther's Original","Smarties,Gummi Worms","Pixie Stix","Gummi Bears","Tootsie Rolls","Blow Pops","Peeps","Tootsie Roll Pops","Hershey Bar","Sour Patch Kids","Necco wafers","Three Musketeers","York Peppermint Patties","Candy corn","Butterfinger","Baby Ruth","Kit-Kat","Pocky","Jelly Belly","Jolly Ranchers","Mr. Goodbar","Nerds","Sugar Daddy","Swedish Fish","M&Ms","Toblerone","Circus peanuts","Skittles","Pop Rocks","Reese's pieces","Candy cigarettes","Twix","Milky Way","Milk Duds","Wax lips","Hershey's Kisses","Cadbury eggs","Starburst","Payday","Reese's Peanut Butter Cups"};
46
+ public:
47
+ void start(){
48
+ cout << "Please choose grid size:" << endl;
49
+ cout << "If you want to play 4*4 grid size, press 4" << endl;
50
+ cout << "If you want to play 6*6 grid size, press 6" << endl;
51
+ cout << "If you want to play 8*8 grid size, press 8" << endl;
52
+ cout << "You choose: ";
53
+ cin >> gridSize;
51
54
  cout << "<---------------------->" << endl;
52
- cout << "テーマを選択して下さい:" << endl;
55
+ cout << "Please choose theme:" << endl;
56
+ cout << "If you want to play element, press 1" << endl;
53
- cout << "元素に関してプレイしたい場合,1を押してください" << endl;
57
+ cout << "If you want to play fruits, press 2" << endl;
54
- cout << "果物に関してプレイしたい場合,2を押してください" << endl;
55
- cout << "お菓子に関してプレイしたい場合,3を押してください" << endl;
58
+ cout << "If you want to play candy, press 3" << endl;
56
- cout << "選んでください: ";
59
+ cout << "You choose: ";
57
60
  cin >> theme;
58
-
61
+ cout << "<---------------------->" << endl;
62
+ cout << "Please choose play spped:" << endl;
63
+ cout << "If you want to play 2seconds(Difficult), press 2" << endl;
64
+ cout << "If you want to play 4seconds(normal), press 4" << endl;
65
+ cout << "If you want to play 6seconds(easy), press 6" << endl;
66
+ cout << "You choose: ";
67
+ cin >> timer;
59
68
 
60
- int **gridArr = new int*[gridsize];
69
+ int **gridArr = new int*[gridSize];
61
- for(int i = 0; i < gridsize; i++) {
70
+ for(int i = 0; i < gridSize; i++) {
62
- gridArr[i] = new int[gridsize];
71
+ gridArr[i] = new int[gridSize];
63
72
  }
64
73
  //いくつ必要なのかを計算する
65
- int randomNum = (gridsize*gridsize)/2;
74
+ int randomNum = (gridSize*gridSize)/2;
66
- int shuffleArr[50];
75
+ int shuffleArr[50];//ただの数字
67
76
  //shuffleArrの中に順に数字を入力する
68
77
  for(int i = 0;i < 50; i++)
69
78
  {
70
79
  shuffleArr[i] = i;
71
80
  }
81
+ //時間を使用した乱数生成を行う
82
+ srand(time(NULL));
72
83
  //ここでランダムに数値を入れ替えていく
73
84
  //後ろから順番に順に交換する.
74
85
  //ここでshuffleArrの中身はシャッフルされる
@@ -88,8 +99,30 @@
88
99
  {
89
100
  tmpArr[i] = shuffleArr[i];
90
101
  };
102
+ //cout << element[tmpArr[i]]でstring elementを呼び出す(後で書く)
91
103
 
104
+ //gridを作成している
105
+ for(int iii = 0; iii<gridSize;iii++)
106
+ {
107
+ cout << " " << iii+1 << " ";
108
+ }
109
+ cout << endl;
110
+ for (int ii = 0; ii <= gridSize; ii++)
111
+ {
112
+ cout << " - ";
113
+ }
114
+ cout << endl;
115
+ for (int i = 0; i < gridSize; i++)
116
+ {
117
+ cout << i + 1 << " | ";
118
+ for (int j = 0; j < gridSize; j++)
119
+ {
120
+ cout << "* ";
121
+ }
122
+ cout << endl;
123
+ }
92
124
 
125
+
93
126
  }
94
127
  };
95
128
  ```

2

1つ目のわからないところを修正しました。

2020/07/25 02:10

投稿

ryamamot0
ryamamot0

スコア3

title CHANGED
File without changes
body CHANGED
@@ -39,10 +39,9 @@
39
39
  int gridSize;
40
40
  int theme;
41
41
  int timer;
42
- string element[50] = {H,He,Li,Be,B,C,N,O,F,Ne,Na,Mg,Al,Si,P,S,Cl,Ar,K,Ca,Sc,Ti,V,Cr,Mn,Fe,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr,Rb,Sr,Y,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn}
43
- string fruits[50] = {grape,apple,tangerine,satsuma,strawberry,pear,apricot,lime,orange,cantaloupe,jujube,papaya,fig,banana,honeydew,cherry,mango,kiwi,tomato,huckleberry,boysenberry,nectarine,grapefruit,pomegranate,passon_fruit,coconut,clementine,date,starfruit,peach,plum,blackberry,kumquat,guava,dragonfruit,raspberry,watermelon,pineapple,blueberry,avocado,lemon}
44
- public:
45
- void start(){
42
+ string element[50] = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn"};
43
+ string fruits[50] = {"grape","apple","tangerine","satsuma","strawberry","pear","apricot","lime","orange","cantaloupe","jujube","papaya","fig","banana","honeydew","cherry","mango","kiwi","tomato","huckleberry","boysenberry","nectarine","grapefruit","pomegranate","passon_fruit","coconut","clementine","date","starfruit","peach","plum","blackberry","kumquat","guava","dragonfruit","raspberry","watermelon","pineapple","blueberry","avocado","lemon"};
44
+ string candy[50] = {"Almond Joy","Cotton candy","Atomic Fireball","Twizzlers","Nik-L-Nips","Jawbreakers","Red Vines","Snickers","Junior Mints","Werther's Original","Smarties,Gummi Worms","Pixie Stix","Gummi Bears","Tootsie Rolls","Blow Pops","Peeps","Tootsie Roll Pops","Hershey Bar","Sour Patch Kids","Necco wafers","Three Musketeers","York Peppermint Patties","Candy corn","Butterfinger","Baby Ruth","Kit-Kat","Pocky","Jelly Belly","Jolly Ranchers","Mr. Goodbar","Nerds","Sugar Daddy","Swedish Fish","M&Ms","Toblerone","Circus peanuts","Skittles","Pop Rocks","Reese's pieces","Candy cigarettes","Twix","Milky Way","Milk Duds","Wax lips","Hershey's Kisses","Cadbury eggs","Starburst","Payday","Reese's Peanut Butter Cups"}; void start(){
46
45
  cout << "グリッドサイズを洗濯して下さい:" << endl;
47
46
  cout << "4*4をプレイしたい場合は4を入力して下さい" << endl;
48
47
  cout << "6*6をプレイしたい場合は6を入力して下さい" << endl;
@@ -85,11 +84,10 @@
85
84
  //tmpArrで1次元配列を作成する
86
85
  int tmpArr[randomNum];
87
86
  //tmpArrの中にランダムに作成した数値を入れる.
88
- //tmpArr[i]
89
87
  for(int i =0; i <randomNum ; i++)
90
88
  {
91
- //ここになんて書けば良いかわからない
89
+ tmpArr[i] = shuffleArr[i];
92
- }
90
+ };
93
91
 
94
92
 
95
93
  }

1

内容の追加

2020/07/25 00:52

投稿

ryamamot0
ryamamot0

スコア3

title CHANGED
File without changes
body CHANGED
@@ -39,6 +39,8 @@
39
39
  int gridSize;
40
40
  int theme;
41
41
  int timer;
42
+ string element[50] = {H,He,Li,Be,B,C,N,O,F,Ne,Na,Mg,Al,Si,P,S,Cl,Ar,K,Ca,Sc,Ti,V,Cr,Mn,Fe,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr,Rb,Sr,Y,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn}
43
+ string fruits[50] = {grape,apple,tangerine,satsuma,strawberry,pear,apricot,lime,orange,cantaloupe,jujube,papaya,fig,banana,honeydew,cherry,mango,kiwi,tomato,huckleberry,boysenberry,nectarine,grapefruit,pomegranate,passon_fruit,coconut,clementine,date,starfruit,peach,plum,blackberry,kumquat,guava,dragonfruit,raspberry,watermelon,pineapple,blueberry,avocado,lemon}
42
44
  public:
43
45
  void start(){
44
46
  cout << "グリッドサイズを洗濯して下さい:" << endl;