質問編集履歴

1

コードの差し替え

2021/02/22 06:15

投稿

NASIJIRU
NASIJIRU

スコア15

test CHANGED
File without changes
test CHANGED
@@ -290,442 +290,442 @@
290
290
 
291
291
  ```ここに言語を入力
292
292
 
293
- #ifndef REVERSI_H_INCLUDED
294
-
295
- #define REVERSI_H_INCLUDED
296
-
297
-
298
-
299
- #include <string>
300
-
301
- #include <sstream>
293
+ #include <iostream>
302
-
294
+
303
- #include <stdexcept>
295
+ #include "Board.h"
304
-
305
-
306
-
296
+
297
+
298
+
307
- const int BOARD_SIZE = 8;
299
+ using namespace std;
308
-
309
- const int MAX_TURNS = 60;
300
+
310
-
311
-
312
-
313
-
314
-
315
- typedef int Color;
301
+
316
-
317
-
318
-
319
- const Color EMPTY = 0;
302
+
320
-
303
+
304
+
321
- const Color WHITE = -1;
305
+ class ConsoleBoard : public Board
322
-
323
- const Color BLACK = 1;
324
-
325
- const Color WALL = 2;
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
- struct Point
334
306
 
335
307
  {
336
308
 
337
-
338
-
339
- int x;
340
-
341
- int y;
342
-
343
-
344
-
345
- Point()
346
-
347
- {
348
-
349
- Point(0, 0);
350
-
351
- }
352
-
353
-
354
-
355
- Point(int x, int y)
356
-
357
- {
358
-
359
- this->x = x;
360
-
361
- this->y = y;
362
-
363
- }
364
-
365
-
366
-
367
- Point(std::string coordstr) noexcept(false)
368
-
369
- {
370
-
371
- if(coordstr.length() < 2)
372
-
373
- throw "The argument must be Reversi style coordinates!";
374
-
375
-
376
-
377
- x = coordstr[0] - 'a'+1;
378
-
379
- y = coordstr[1] - '1'+1;
380
-
381
- }
382
-
383
-
384
-
385
- operator std::string() const
386
-
387
- {
388
-
389
- std::ostringstream oss;
390
-
391
- oss << static_cast<char>('a'+x-1) << static_cast<char>('1'+y-1);
392
-
393
-
394
-
395
- return oss.str();
396
-
397
- }
398
-
399
-
400
-
401
- bool operator==(const Point& p) const
402
-
403
- {
404
-
405
- if(x != p.x) return false;
406
-
407
- if(y != p.y) return false;
408
-
409
-
410
-
411
- return true;
309
+ public:
310
+
311
+ void print()
312
+
313
+ {
314
+
315
+ cout << " a b c d e f g h " << endl;
316
+
317
+ for(int y=1; y<=8; y++)
318
+
319
+ {
320
+
321
+ cout << " " << y;
322
+
323
+ for(int x=1; x<=8; x++)
324
+
325
+ {
326
+
327
+ switch(getColor(Point(x, y)))
328
+
329
+ {
330
+
331
+ case BLACK:
332
+
333
+ cout << "●";
334
+
335
+ break;
336
+
337
+ case WHITE:
338
+
339
+ cout << "◯";
340
+
341
+ break;
342
+
343
+ default:
344
+
345
+ cout << " ";
346
+
347
+ break;
348
+
349
+ }
350
+
351
+ }
352
+
353
+ cout << endl;
354
+
355
+ }
412
356
 
413
357
  }
414
358
 
415
359
  };
416
360
 
417
- /*
361
+
418
-
362
+
419
- bool operator==(const Point& lhs, const Point& rhs)
363
+ ostream& operator<<(ostream& os, const Point& p)
420
364
 
421
365
  {
422
366
 
367
+ string s = p;
368
+
423
- if(lhs.x != rhs.x) return false;
369
+ os << s;
424
-
425
- if(lhs.y != rhs.y) return false;
370
+
426
-
427
-
428
-
429
- return true;
371
+ return os;
372
+
430
-
373
+ }
374
+
375
+
376
+
431
- }*/
377
+ int main()
432
-
433
-
434
-
435
- struct Disc : public Point
436
378
 
437
379
  {
438
380
 
439
- Color color;
440
-
441
-
442
-
443
- Disc() : Point(0, 0)
444
-
445
- {
446
-
447
- color = EMPTY;
448
-
449
- }
450
-
451
-
452
-
453
- Disc(int x, int y, Color color) : Point(x, y)
454
-
455
- {
456
-
457
- this->color = color;
458
-
459
- }
381
+ ConsoleBoard board;
382
+
383
+
384
+
385
+ while(true)
386
+
387
+ {
388
+
389
+ board.print();
390
+
391
+ cout << "黒石" << board.countDisc(BLACK) << " ";
392
+
393
+ cout << "白石" << board.countDisc(WHITE) << " ";
394
+
395
+ cout << "空きマス" << board.countDisc(EMPTY) << endl;
396
+
397
+
398
+
399
+
400
+
401
+ vector<Point> movables = board.getMovablePos();
402
+
403
+ for(unsigned i=0; i<movables.size(); i++)
404
+
405
+ {
406
+
407
+ cout << movables[i] << " ";
408
+
409
+ }
410
+
411
+ cout << endl << endl;
412
+
413
+
414
+
415
+ cout << "手を入力してください";
416
+
417
+ Point p;
418
+
419
+
420
+
421
+ string in;
422
+
423
+ cin >> in;
424
+
425
+
426
+
427
+ if(in == "p")
428
+
429
+ {
430
+
431
+
432
+
433
+ if(!board.pass())
434
+
435
+ cerr << "パスできません" << endl;
436
+
437
+
438
+
439
+ continue;
440
+
441
+ }
442
+
443
+
444
+
445
+ if(in == "u")
446
+
447
+ {
448
+
449
+ // undo
450
+
451
+ board.undo();
452
+
453
+ continue;
454
+
455
+ }
456
+
457
+
458
+
459
+ try
460
+
461
+ {
462
+
463
+
464
+
465
+ Point parse(in);
466
+
467
+ p = parse;
468
+
469
+ }
470
+
471
+ catch(const char* e)
472
+
473
+ {
474
+
475
+ cerr << "リバーシ形式の手を入力してください!" << endl;
476
+
477
+ continue;
478
+
479
+ }
480
+
481
+
482
+
483
+ if(board.move(p) == false)
484
+
485
+ {
486
+
487
+ cerr << "そこには置けません" << endl;
488
+
489
+ continue;
490
+
491
+ }
492
+
493
+
494
+
495
+ if(board.isGameOver())
496
+
497
+ {
498
+
499
+ cout << "----------------ゲーム終了----------------" << endl;
500
+
501
+ return 0;
502
+
503
+ }
504
+
505
+ }
506
+
507
+
508
+
509
+ return 0;
510
+
511
+
512
+
513
+ }
514
+
515
+
516
+
517
+ ```
518
+
519
+
520
+
521
+
522
+
523
+ Board.h
524
+
525
+ ```ここに言語を入力
526
+
527
+ #ifndef BOARD_H_INCLUDED
528
+
529
+ #define BOARD_H_INCLUDED
530
+
531
+
532
+
533
+ #include "Reversi.h"
534
+
535
+ #include <vector>
536
+
537
+
538
+
539
+ class Board
540
+
541
+ {
542
+
543
+ public:
544
+
545
+ Board();
546
+
547
+
548
+
549
+ void init();
550
+
551
+ bool move(const Point& point);
552
+
553
+ bool pass();
554
+
555
+ bool undo();
556
+
557
+ bool isGameOver() const;
558
+
559
+
560
+
561
+ unsigned countDisc(Color color) const
562
+
563
+ {
564
+
565
+ return Discs[color];
566
+
567
+ }
568
+
569
+
570
+
571
+ Color getColor(const Point& p) const
572
+
573
+ {
574
+
575
+ return RawBoard[p.x][p.y];
576
+
577
+ }
578
+
579
+
580
+
581
+ const std::vector<Point>& getMovablePos() const
582
+
583
+ {
584
+
585
+ return MovablePos[Turns];
586
+
587
+ }
588
+
589
+
590
+
591
+ std::vector<Disc> getUpdate() const
592
+
593
+ {
594
+
595
+ if(UpdateLog.empty()) return std::vector<Disc>();
596
+
597
+ else return UpdateLog.back();
598
+
599
+ }
600
+
601
+
602
+
603
+ Color getCurrentColor() const
604
+
605
+ {
606
+
607
+ return CurrentColor;
608
+
609
+ }
610
+
611
+
612
+
613
+ unsigned getTurns() const
614
+
615
+ {
616
+
617
+ return Turns;
618
+
619
+ }
620
+
621
+
622
+
623
+ std::vector<Point> getHistory() const
624
+
625
+ {
626
+
627
+ std::vector<Point> history;
628
+
629
+
630
+
631
+ for(unsigned i=0; i<UpdateLog.size(); i++)
632
+
633
+ {
634
+
635
+ std::vector<Disc> update = UpdateLog[i];
636
+
637
+ if(update.empty()) continue;
638
+
639
+ history.push_back(update[0]);
640
+
641
+ }
642
+
643
+
644
+
645
+ return history;
646
+
647
+ }
648
+
649
+
650
+
651
+ unsigned getLiberty(const Point& p) const
652
+
653
+ {
654
+
655
+ return Liberty[p.x][p.y];
656
+
657
+ }
658
+
659
+
660
+
661
+
662
+
663
+ private:
664
+
665
+
666
+
667
+
668
+
669
+ static const unsigned NONE = 0;
670
+
671
+ static const unsigned UPPER = 1;
672
+
673
+ static const unsigned UPPER_LEFT = 2;
674
+
675
+ static const unsigned LEFT = 4;
676
+
677
+ static const unsigned LOWER_LEFT = 8;
678
+
679
+ static const unsigned LOWER = 16;
680
+
681
+ static const unsigned LOWER_RIGHT = 32;
682
+
683
+ static const unsigned RIGHT = 64;
684
+
685
+ static const unsigned UPPER_RIGHT = 128;
686
+
687
+
688
+
689
+
690
+
691
+ Color RawBoard[BOARD_SIZE+2][BOARD_SIZE+2];
692
+
693
+ unsigned Turns;
694
+
695
+ Color CurrentColor;
696
+
697
+
698
+
699
+ std::vector<std::vector<Disc> > UpdateLog;
700
+
701
+
702
+
703
+ std::vector<Point> MovablePos[MAX_TURNS+1];
704
+
705
+ unsigned MovableDir[MAX_TURNS+1][BOARD_SIZE+2][BOARD_SIZE+2];
706
+
707
+ unsigned Liberty[BOARD_SIZE+2][BOARD_SIZE+2];
708
+
709
+
710
+
711
+ ColorStorage<unsigned> Discs;
712
+
713
+
714
+
715
+ void flipDiscs(const Point& point);
716
+
717
+ unsigned checkMobility(const Disc& disc) const;
718
+
719
+ void initMovable();
720
+
721
+
460
722
 
461
723
  };
462
724
 
463
725
 
464
726
 
465
- template<typename T> class ColorStorage
466
-
467
- {
468
-
469
- T data[3];
470
-
471
- public:
472
-
473
- T& operator[](int color)
474
-
475
- {
476
-
477
- return data[color+1];
478
-
479
- }
480
-
481
-
482
-
483
- const T& operator[](int color) const
484
-
485
- {
486
-
487
- return data[color+1];
488
-
489
- }
490
-
491
-
492
-
493
- ColorStorage<T>& operator +=(const ColorStorage<T> &src)
494
-
495
- {
496
-
497
- data[0] += src.data[0];
498
-
499
- data[1] += src.data[1];
500
-
501
- data[2] += src.data[2];
502
-
503
-
504
-
505
- return *this;
506
-
507
- }
508
-
509
-
510
-
511
- };
512
-
513
-
514
-
515
727
  #endif
516
728
 
729
+
730
+
517
731
  ```
518
-
519
-
520
-
521
-
522
-
523
- Board.h
524
-
525
- ```ここに言語を入力
526
-
527
- #ifndef BOARD_H_INCLUDED
528
-
529
- #define BOARD_H_INCLUDED
530
-
531
-
532
-
533
- #include "Reversi.h"
534
-
535
- #include <vector>
536
-
537
-
538
-
539
- class Board
540
-
541
- {
542
-
543
- public:
544
-
545
- Board();
546
-
547
-
548
-
549
- void init();
550
-
551
- bool move(const Point& point);
552
-
553
- bool pass();
554
-
555
- bool undo();
556
-
557
- bool isGameOver() const;
558
-
559
-
560
-
561
- unsigned countDisc(Color color) const
562
-
563
- {
564
-
565
- return Discs[color];
566
-
567
- }
568
-
569
-
570
-
571
- Color getColor(const Point& p) const
572
-
573
- {
574
-
575
- return RawBoard[p.x][p.y];
576
-
577
- }
578
-
579
-
580
-
581
- const std::vector<Point>& getMovablePos() const
582
-
583
- {
584
-
585
- return MovablePos[Turns];
586
-
587
- }
588
-
589
-
590
-
591
- std::vector<Disc> getUpdate() const
592
-
593
- {
594
-
595
- if(UpdateLog.empty()) return std::vector<Disc>();
596
-
597
- else return UpdateLog.back();
598
-
599
- }
600
-
601
-
602
-
603
- Color getCurrentColor() const
604
-
605
- {
606
-
607
- return CurrentColor;
608
-
609
- }
610
-
611
-
612
-
613
- unsigned getTurns() const
614
-
615
- {
616
-
617
- return Turns;
618
-
619
- }
620
-
621
-
622
-
623
- std::vector<Point> getHistory() const
624
-
625
- {
626
-
627
- std::vector<Point> history;
628
-
629
-
630
-
631
- for(unsigned i=0; i<UpdateLog.size(); i++)
632
-
633
- {
634
-
635
- std::vector<Disc> update = UpdateLog[i];
636
-
637
- if(update.empty()) continue;
638
-
639
- history.push_back(update[0]);
640
-
641
- }
642
-
643
-
644
-
645
- return history;
646
-
647
- }
648
-
649
-
650
-
651
- unsigned getLiberty(const Point& p) const
652
-
653
- {
654
-
655
- return Liberty[p.x][p.y];
656
-
657
- }
658
-
659
-
660
-
661
-
662
-
663
- private:
664
-
665
-
666
-
667
-
668
-
669
- static const unsigned NONE = 0;
670
-
671
- static const unsigned UPPER = 1;
672
-
673
- static const unsigned UPPER_LEFT = 2;
674
-
675
- static const unsigned LEFT = 4;
676
-
677
- static const unsigned LOWER_LEFT = 8;
678
-
679
- static const unsigned LOWER = 16;
680
-
681
- static const unsigned LOWER_RIGHT = 32;
682
-
683
- static const unsigned RIGHT = 64;
684
-
685
- static const unsigned UPPER_RIGHT = 128;
686
-
687
-
688
-
689
-
690
-
691
- Color RawBoard[BOARD_SIZE+2][BOARD_SIZE+2];
692
-
693
- unsigned Turns;
694
-
695
- Color CurrentColor;
696
-
697
-
698
-
699
- std::vector<std::vector<Disc> > UpdateLog;
700
-
701
-
702
-
703
- std::vector<Point> MovablePos[MAX_TURNS+1];
704
-
705
- unsigned MovableDir[MAX_TURNS+1][BOARD_SIZE+2][BOARD_SIZE+2];
706
-
707
- unsigned Liberty[BOARD_SIZE+2][BOARD_SIZE+2];
708
-
709
-
710
-
711
- ColorStorage<unsigned> Discs;
712
-
713
-
714
-
715
- void flipDiscs(const Point& point);
716
-
717
- unsigned checkMobility(const Disc& disc) const;
718
-
719
- void initMovable();
720
-
721
-
722
-
723
- };
724
-
725
-
726
-
727
- #endif
728
-
729
-
730
-
731
- ```