Advertisement
Guest User

Untitled

a guest
May 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. function setup() {
  2. createCanvas(16000, 16000);
  3. noLoop();
  4. }
  5.  
  6. function draw() {
  7. background(220);
  8. drawSquare([0,0,0,0,0,0,0,0,0],0,0,width,height, 0);
  9. }
  10.  
  11. function drawSquare(config, x,y,w,h, turn) {
  12. if (turn == 9) {
  13. return
  14. }
  15.  
  16. line(floor(x+w*0.333), floor(y), floor(x+w*0.333), floor(y+h));
  17. line(floor(x+w*0.666), floor(y), floor(x+w*0.666), floor(y+h));
  18. line(floor(x), floor(y + h*0.333), floor(x+w), floor(y+h*0.333));
  19. line(floor(x), floor(y+h*0.666), floor(x+w), floor(y+h*0.666));
  20. var mw = w*0.333-4;
  21. var mh = h*0.333-4;
  22. var winConfig = findWin(config, turn);
  23. for(var i = 0; i<9; i++) {
  24. var cx = x + (i%3)*w*0.333+2;
  25. var cy = y + floor(i/3)*h*0.333+2;
  26. //if (winConfig >= 0) {
  27. // if (winConfig != i) {
  28. // continue;
  29. // }
  30. //}
  31. if (config[i] != 0 || lineFound(config)) {
  32. drawShape(floor(cx),floor(cy), floor(mw), floor(mh), config[i]);
  33. } else {
  34.  
  35. var newConf = config.slice();
  36. newConf[i] = (turn%2)+1
  37. drawSquare(newConf, cx, cy , mw, mh, turn+1)
  38. }
  39. }
  40. }
  41.  
  42. function drawShape(x,y,w, h, shape) {
  43. if (shape==0) {
  44. return;
  45. }
  46. noFill();
  47.  
  48. if (shape==1) {
  49. stroke("red");
  50. if (w>3) {
  51. ellipse(x+w*0.5, y+h*0.5,w*0.8, h*0.8)
  52.  
  53. } else {
  54. point(x+w*0.5,y+w*0.5);
  55. }
  56. } else {
  57. stroke("blue")
  58. if (w>3) {
  59. line(x+w, y+h, x, y);
  60. line(x+w, y, x, y+h);
  61. } else {
  62. point(x+w*0.5,y+w*0.5);
  63. }
  64.  
  65. }
  66. stroke("black");
  67. }
  68.  
  69. function findWin(config, turn) {
  70. for(var i = 0; i<9; i++) {
  71.  
  72. var newConf = config.slice();
  73. newConf[i] = (turn%2)+1
  74. if(lineFound(newConf)) {
  75. return i;
  76. }
  77. }
  78. return -1;
  79. }
  80.  
  81. function lineFound(config) {
  82. return (config[0] == config[1] && config[0] == config[2] && config[0] > 0) ||
  83. (config[3] == config[4] && config[3] == config[5] && config[3] > 0) ||
  84. (config[6] == config[7] && config[6] == config[8] && config[6] > 0) ||
  85. (config[0] == config[3] && config[0] == config[6] && config[0] > 0) ||
  86. (config[1] == config[4] && config[1] == config[7] && config[1] > 0) ||
  87. (config[2] == config[5] && config[2] == config[8] && config[2] > 0) ||
  88. (config[0] == config[4] && config[0] == config[8] && config[0] > 0) ||
  89. (config[2] == config[4] && config[2] == config[6] && config[2] > 0)
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement