Advertisement
Guest User

chainreationc++console

a guest
Jun 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int boardsize;
  6. class some{
  7. public:
  8. int x;
  9. int y;
  10. int cl;
  11. int num;
  12. int max;
  13. };
  14.  
  15.  
  16. some squares[5][5];
  17. int turn = 0;
  18. int cordavez = 0, qdejogadores = 3;
  19. bool jogando = true;
  20. bool Putball(int x, int y, int cor);
  21. void Corrector();
  22. bool Verify();
  23. void Print();
  24. int Findwinners();
  25. bool Isdead(int cor);
  26. int main()
  27. {
  28. boardsize = 5;
  29. int i =0;
  30. int j =0;
  31. for(i = 0; i < boardsize;i++){
  32.  
  33. for(j=0; j<boardsize; j++){
  34. squares[i][j].cl= -1;
  35. squares[i][j].x = i;
  36. squares[i][j].y = j;
  37. squares[i][j].num = 0;
  38. squares[i][j].max = 3;
  39. if(i == boardsize || i == 0 || j == boardsize || j == 0){
  40. squares[i][j].max = 2;
  41.  
  42. }
  43.  
  44. if(i == boardsize && j == boardsize || i == boardsize && j == 0|| i==0 && j ==0 || i==0 && j== boardsize){
  45. squares[i][j].max = 1;
  46.  
  47. }
  48.  
  49.  
  50. }
  51. }
  52. while(jogando)
  53. {
  54. if(Findwinners() != -1){
  55. break;
  56. }
  57. while(Verify())
  58. {
  59. Corrector();
  60. Print();
  61. }
  62. Print();
  63. int xescolhido, yescolhido;
  64. Corrector();
  65. while(Isdead(cordavez)){
  66. cordavez++;
  67. if(cordavez > qdejogadores )
  68. {
  69. cordavez = 0;
  70. }
  71.  
  72. }
  73. cout <<"jogador " ;
  74. cout << cordavez;
  75. cout << " sua vez";
  76. cin >> xescolhido;
  77. cin >> yescolhido;
  78. bool validmove;
  79.  
  80. validmove = Putball(xescolhido,yescolhido,cordavez);
  81.  
  82. if(validmove == true){
  83. cordavez++;
  84. turn++;
  85. }
  86. if(cordavez > qdejogadores)
  87. {
  88. cordavez = 0;
  89. }
  90. }
  91. cout <<"jogador " ;
  92. cout << Findwinners();
  93. cout << " ganha";
  94. system("PAUSE");
  95. }
  96. bool Putball(int x, int y, int cor)
  97. {
  98. int nextright,nextleft,nextup,nextdown;
  99. nextright = x+1;
  100. nextleft = x-1;
  101. nextup = y+1;
  102. nextdown = y-1;
  103. if(x>boardsize || x < 0 || y > boardsize || y<0){
  104. return false;
  105. }
  106. if(squares[x][y].num == 0 || (squares[x][y].num != 0 && squares[x][y].cl == cor))
  107. {
  108. squares[x][y].num++;
  109. squares[x][y].cl = cor;
  110. if(squares[x][y].num > squares[x][y].max)
  111. {
  112. squares[x][y].num = 0;
  113. if(nextright < 0 || nextright > boardsize)
  114. {
  115.  
  116. }else{
  117. squares[x+1][y].num++;
  118. squares[x+1][y].cl = cor;
  119. }
  120. if(nextleft < 0 || nextleft > boardsize)
  121. {
  122.  
  123. }else{
  124. squares[x-1][y].num++;
  125. squares[x-1][y].cl = cor;
  126. }
  127. if(nextup < 0 || nextup > boardsize)
  128. {
  129.  
  130. }else{
  131. squares[x][y+1].num++;
  132. squares[x][y+1].cl = cor;
  133. }
  134.  
  135. if(nextdown < 0 || nextdown > boardsize)
  136. {
  137.  
  138. }else{
  139. squares[x][y-1].num++;
  140. squares[x][y-1].cl = cor;
  141. }
  142.  
  143.  
  144. }
  145. return true;
  146. }else{
  147. return false;
  148. }
  149.  
  150.  
  151. }
  152. void Corrector()
  153. {
  154. bool exit = false;
  155. int i,j;
  156. for(i = 0; i < boardsize;i++){
  157.  
  158. for(j=0; j<boardsize; j++){
  159. int nextright,nextleft,nextup,nextdown;
  160. int x,y;
  161. x = i;
  162. y = j;
  163. nextright = x+1;
  164. nextleft = x-1;
  165. nextup = y+1;
  166. nextdown = y-1;
  167. if(squares[i][j].num > squares[i][j].max)
  168. {
  169. squares[x][y].num = 0;
  170. if(nextright < 0 || nextright > boardsize)
  171. {
  172.  
  173. }else{
  174. squares[x+1][y].num++;
  175. squares[x+1][y].cl = cordavez-1;
  176. }
  177. if(nextleft < 0 || nextleft > boardsize)
  178. {
  179.  
  180. }else{
  181. squares[x-1][y].num++;
  182. squares[x-1][y].cl = cordavez-1;
  183. }
  184. if(nextup < 0 || nextup > boardsize)
  185. {
  186.  
  187. }else{
  188. squares[x][y+1].num++;
  189. squares[x][y+1].cl = cordavez-1;
  190. }
  191.  
  192. if(nextdown < 0 || nextdown > boardsize)
  193. {
  194.  
  195. }else{
  196. squares[x][y-1].num++;
  197. squares[x][y-1].cl = cordavez-1;
  198. } //Putball(i,j,squares[i][j].cl);
  199. exit = true;
  200. break;
  201. }
  202.  
  203.  
  204. }
  205. if(exit)
  206. {
  207. break;
  208. }
  209. }
  210.  
  211.  
  212.  
  213. }
  214. bool Verify()
  215. {
  216. bool exit = false;
  217. int i,j;
  218. for(i = 0; i < boardsize;i++){
  219.  
  220. for(j=0; j<boardsize; j++){
  221.  
  222. if(squares[i][j].num > squares[i][j].max)
  223. {
  224. //Putball(i,j,squares[i][j].cl);
  225. exit = true;
  226. break;
  227. }
  228.  
  229.  
  230. }
  231. if(exit)
  232. {
  233. break;
  234.  
  235. }
  236. }
  237. if(exit){
  238. return true;
  239. }else{
  240. return false;
  241. }
  242.  
  243.  
  244. }
  245. void Print(){
  246. int i,j;
  247. for(i = 0; i < boardsize;i++){
  248.  
  249. for(j=0; j<boardsize; j++){
  250. cout << squares[i][j].num;
  251. cout << " c > " ;
  252. cout << squares[i][j].cl;
  253. cout << " " ;
  254. }
  255. cout << "" << endl;
  256. }
  257.  
  258. }
  259. int Findwinners()
  260. {
  261. if(turn < qdejogadores)
  262. {
  263. return -1;
  264. }
  265. bool clexist[qdejogadores];
  266. bool anothercontenderisalive = false;
  267. bool nowinner = false;
  268. int i,j,k,p,g, winner;
  269. for(k = 0; k < qdejogadores; k++)
  270. {
  271. clexist[k] = false;
  272. }
  273. for(i = 0; i < boardsize;i++){
  274.  
  275. for(j=0; j<boardsize; j++){
  276. if(squares[i][j].cl != -1)
  277. {
  278. clexist[squares[i][j].cl] = true;
  279. }
  280.  
  281.  
  282. }
  283.  
  284. }
  285. for(p = 0; p < qdejogadores;p++)
  286. {
  287. if(clexist[p] == true){
  288. winner = p;
  289. for(g = 0; g < qdejogadores; g++)
  290. {
  291. if(p != g)
  292. {
  293. if(clexist[p] == clexist[g])
  294. {
  295. nowinner = true;
  296.  
  297. }
  298. }
  299.  
  300. }
  301.  
  302.  
  303.  
  304. }
  305.  
  306.  
  307. }
  308. if(!nowinner)
  309. {
  310. return winner;
  311. }else{
  312. return -1;
  313. }
  314.  
  315.  
  316. }
  317. bool Isdead(int cor)
  318. {
  319. bool exit = false;
  320. int i,j;
  321. if(turn <= qdejogadores)
  322. {
  323. return false;
  324. }
  325. for(i = 0; i < boardsize;i++){
  326.  
  327. for(j=0; j<boardsize; j++){
  328. if(squares[i][j].cl == cor)
  329. {
  330. exit = true;
  331. break;
  332. }
  333.  
  334.  
  335.  
  336.  
  337. }
  338. if(exit)
  339. {
  340. break;
  341. }
  342. }
  343. if(exit)
  344. {
  345. return false;
  346. }else{
  347. return true;
  348. }
  349.  
  350.  
  351.  
  352.  
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement