Viergewinnt (four-in-a-row) implementation in c++ and QT
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

renderarea.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* VierGewinnt - A simple 4-in-a-row network game
  2. *
  3. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  4. * http://www.seba-geek.de
  5. *
  6. * This file is part of VierGewinnt.
  7. *
  8. * VierGewinnt is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * VierGewinnt is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with VierGewinnt. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "renderarea.h"
  22. #include <QPainter>
  23. RenderArea::RenderArea(QWidget *parent) : QWidget(parent) {
  24. QSizePolicy tmppol;
  25. tmppol.setHorizontalPolicy(QSizePolicy::Maximum);
  26. tmppol.setVerticalPolicy(QSizePolicy::Maximum);
  27. setSizePolicy(tmppol);
  28. // this->antialiased = true;
  29. // pens
  30. xpen.setColor(Qt::green);
  31. xpen.setWidth(10);
  32. xpen.setJoinStyle(Qt::RoundJoin);
  33. xpen.setCapStyle(Qt::RoundCap);
  34. ypen.setColor(Qt::red);
  35. ypen.setWidth(10);
  36. mfact = 0.75;
  37. feld = 0; // just to clarify, also set in next statement
  38. setFeldInfo(7, 6, 0);
  39. }
  40. void RenderArea::setFeldInfo(int _x, int _y, VierGewinntWidget::Stein **_feld) {
  41. fx = _x;
  42. fy = _y;
  43. feld = _feld;
  44. }
  45. QSize RenderArea::minimumSizeHint() const {
  46. return QSize(320, 240);
  47. }
  48. QSize RenderArea::sizeHint() const {
  49. return QSize(640, 480);
  50. }
  51. void RenderArea::paintEvent(QPaintEvent * /*event*/) {
  52. QPainter painter(this);
  53. painter.setRenderHint(QPainter::Antialiasing, true);
  54. // Start with grid
  55. int fsize = std::min(width()/fx, height()/fy);
  56. // painter.drawLine(0, 0, width(), height());
  57. painter.translate((width()-fsize*fx)/2, (height()-fsize*fy)/2);
  58. for(int i=0; i<=fx; i++)
  59. painter.drawLine(i*fsize, 0, i*fsize, fsize*fy);
  60. for(int i=0; i<=fy; i++)
  61. painter.drawLine(0, i*fsize, fsize*fx, i*fsize);
  62. if(feld==0)
  63. return;
  64. // paint x and o
  65. for(int i=0; i<fx; i++) {
  66. for(int j=0; j<fy; j++) {
  67. if(feld[i][j]!=VierGewinntWidget::n) {
  68. painter.save();
  69. painter.translate(i*fsize+fsize/2, j*fsize+fsize/2);
  70. if(feld[i][j]==VierGewinntWidget::x) {
  71. painter.setPen(xpen);
  72. painter.drawLine(-fsize/2*mfact, -fsize/2*mfact, fsize/2*mfact, fsize/2*mfact);
  73. painter.drawLine(-fsize/2*mfact, fsize/2*mfact, fsize/2*mfact,-fsize/2*mfact);
  74. } else {
  75. painter.setPen(ypen);
  76. painter.drawEllipse(-fsize/2*mfact, -fsize/2*mfact, fsize*mfact, fsize*mfact);
  77. }
  78. painter.restore();
  79. }
  80. }
  81. }
  82. }
  83. void RenderArea::mouseReleaseEvent(QMouseEvent *event) {
  84. if(event->button()!=Qt::LeftButton)
  85. return;
  86. int fsize = std::min(width()/fx, height()/fy);
  87. int myrow = (event->x()-((width()-fsize*fx)/2))/fsize;
  88. if(myrow<0||myrow>=fx||event->x()-((width()-fsize*fx)/2)<0)
  89. return;
  90. addStein(myrow);
  91. }