現在、CUI上で行う神経衰弱を作成しています。
単語を50個用意し、ランダムにその中から単語を抽出し、並べています。
私は現在、0-49までの数値を別個で作成し、そこからランダムな数値を排出して上で、その数値をテーマによる配列の数字と合致させて行いたいと思っています。
今行いたいこと
・tmpArrに数値をgridsize分入力させる
・gridsizeに入っている数を2倍(数値を2倍ではない)にする
・ランダムに2次元配列に挿入する
どうかご教授下さい。よろしくお願いします。
現在記述しているコード
C++
1//main.cpp 2#include <iostream> 3using namespace std; 4#include <string> 5#include "memorymatchinggame.h" 6 7 8int main() 9{ 10 memorymatchinggame Game; 11 Game.start(); 12 return 0; 13}
C++
1//MemoryMatchGame.h 2 3#include <iostream> 4#include <vector> 5#include <string> 6#include <random> 7#include <numeric> 8#include <cassert> 9#include <time.h> 10#include <iomanip> 11using namespace std; 12 13class MemoryMatchGame 14{ 15private: 16 int gridSize; 17 int theme; 18 int timer; 19 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"}; 20 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"}; 21 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"}; 22public: 23 void start(){ 24 cout << "Please choose grid size:" << endl; 25 cout << "If you want to play 4*4 grid size, press 4" << endl; 26 cout << "If you want to play 6*6 grid size, press 6" << endl; 27 cout << "If you want to play 8*8 grid size, press 8" << endl; 28 cout << "You choose: "; 29 cin >> gridSize; 30 cout << "<---------------------->" << endl; 31 cout << "Please choose theme:" << endl; 32 cout << "If you want to play element, press 1" << endl; 33 cout << "If you want to play fruits, press 2" << endl; 34 cout << "If you want to play candy, press 3" << endl; 35 cout << "You choose: "; 36 cin >> theme; 37 cout << "<---------------------->" << endl; 38 cout << "Please choose play spped:" << endl; 39 cout << "If you want to play 2seconds(Difficult), press 2" << endl; 40 cout << "If you want to play 4seconds(normal), press 4" << endl; 41 cout << "If you want to play 6seconds(easy), press 6" << endl; 42 cout << "You choose: "; 43 cin >> timer; 44 45 int **gridArr = new int*[gridSize]; 46 for(int i = 0; i < gridSize; i++) { 47 gridArr[i] = new int[gridSize]; 48 } 49 //いくつ必要なのかを計算する 50 int randomNum = (gridSize*gridSize)/2; 51 int shuffleArr[50];//ただの数字 52 //shuffleArrの中に順に数字を入力する 53 for(int i = 0;i < 50; i++) 54 { 55 shuffleArr[i] = i; 56 } 57 //時間を使用した乱数生成を行う 58 srand(time(NULL)); 59 //ここでランダムに数値を入れ替えていく 60 //後ろから順番に順に交換する. 61 //ここでshuffleArrの中身はシャッフルされる 62 /* 63 for (int i = 50; i > 1; --i) 64 { 65 int a = i-1; 66 int b = rand()%i; 67 //change shuffleArr[a] and ShuffleArr[b] 68 int tmp = shuffleArr[a]; 69 shuffleArr[a] = shuffleArr[b]; 70 shuffleArr[b] = tmp; 71 } 72 */ 73 //tmpArrで1次元配列を作成する 74 int tmpArr[randomNum]; 75 //tmpArrの中にランダムに作成した数値を入れる. 76 for(int i =0; i <randomNum ; i++) 77 { 78 tmpArr[i] = shuffleArr[i]; 79 }; 80 //cout << element[tmpArr[i]]でstring elementを呼び出す(後で書く) 81 82 83 assert( gridSize*gridSize % 2 == 0 ); 84 int num = gridSize * gridSize / 2; 85 int* data = new int[num*2]; 86 // 「0~num-1 で埋める」を二回やって 87 iota(data, data+num, 0); 88 iota(data+num, data+num*2, 0); 89 // かきまぜる 90 for (int i = 50; i > 1; --i) 91 { 92 int a = i-1; 93 int b = rand()%i; 94 //change shuffleArr[a] and ShuffleArr[b] 95 int tmp = shuffleArr[a]; 96 shuffleArr[a] = shuffleArr[b]; 97 shuffleArr[b] = tmp; 98 } 99 100 101 for (int r = 0; r < gridSize; ++r) 102 { 103 for (int c = 0; c < gridSize; ++c) 104 { 105 cout << gridArr[r][c] << ' '; 106 } 107 cout << endl; 108 } 109 110 111 //gridを作成している 112 for(int iii = 0; iii<gridSize;iii++) 113 { 114 cout << " " << iii+1 << " "; 115 } 116 cout << endl; 117 for (int ii = 0; ii <= gridSize; ii++) 118 { 119 cout << " - "; 120 } 121 cout << endl; 122 for (int i = 0; i < gridSize; i++) 123 { 124 cout << i + 1 << " | "; 125 for (int j = 0; j < gridSize; j++) 126 { 127 cout << "* "; 128 } 129 cout << endl; 130 } 131 132 133 } 134}; 135
回答2件
あなたの回答
tips
プレビュー