private void button1_Click(object sender, EventArgs e) { if ((button1.BackgroundImage == null) && (endState == false)) { AddSign b1 = new AddSign(lastSign, button1); lastSign = b1.GetSign(); playField[0] = b1.GetSign(); gameEnd = b1.checkWin(playField); if ((gameEnd == 'X') || (gameEnd == 'O') || (gameEnd == 'D')) { EndOfGame(); } if (vsAI == true) { ai.AITurn(); lastSign = ai.GetLastSign(); if ((ai.GetGameEnd() == 'X') || (ai.GetGameEnd() == 'O') || (ai.GetGameEnd() == 'D')) { gameEnd = ai.GetGameEnd(); EndOfGame(); } } } }
private bool checkIfAbleToWin() { int counter = 0; int oCounter = 0, xCounter = 0; for (int i = 0; i < 3; i++) //HORIZONTAL LINES { for (int j = 0; j < 3; j++) { counter++; if (playField2d[i, j] == playerSign) { oCounter++; } if (playField2d[i, j] == botSign) { xCounter++; } } if ((xCounter == 2) && (oCounter == 0)) { for (int j = 0; j < 3; j++) { if ((playField2d[i, j] != playerSign) && (playField2d[i, j] != botSign)) { AddSign b = new AddSign(playerSign, buttonList[counter - Math.Abs(3 - j)]); lastSign = b.GetSign(); playField2d[i, j] = botSign; updateArray(); gameEnd = b.checkWin(playField); return(true); } } } xCounter = 0; oCounter = 0; } counter = 0; xCounter = 0; oCounter = 0; //vertical lines for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { counter++; if (playField2d[j, i] == botSign) { xCounter++; } if (playField2d[j, i] == playerSign) { oCounter++; } } if ((xCounter == 2) && (oCounter == 0)) { for (int j = 0; j < 3; j++) { if ((playField2d[j, i] != playerSign) && playField2d[j, i] != botSign) { AddSign b = new AddSign(playerSign, buttonList[3 * (j + 1) - Math.Abs(3 - i)]); lastSign = b.GetSign(); playField2d[j, i] = botSign; updateArray(); gameEnd = b.checkWin(playField); } } return(true); } xCounter = 0; oCounter = 0; } counter = 0; xCounter = 0; oCounter = 0; for (int i = 0; i < 3; i++) /* 1 5 9 buttons */ { if (playField2d[i, i] == playerSign) { oCounter++; } if (playField2d[i, i] == botSign) { xCounter++; } } if ((xCounter == 2) && (oCounter == 0)) { for (int i = 0; i < 3; i++) { if (playField2d[i, i] != botSign) { AddSign b = new AddSign(playerSign, buttonList[(i + 1) * 3 - Math.Abs(3 - i)]); lastSign = b.GetSign(); playField2d[i, i] = botSign; updateArray(); gameEnd = b.checkWin(playField); return(true); } } } xCounter = 0; oCounter = 0; counter = 0; for (int i = 0; i < 3; i++) /* 3 5 7 buttons */ { if (playField2d[i, 2 - i] == playerSign) { oCounter++; } if (playField2d[i, 2 - i] == botSign) { xCounter++; } } if ((xCounter == 2) && (oCounter == 0)) { for (int i = 0; i < 3; i++) { if (playField2d[i, 2 - i] != botSign) { AddSign b = new AddSign(playerSign, buttonList[(i * 3 + (3 - i)) - 1]); lastSign = b.GetSign(); playField2d[i, 2 - i] = botSign; updateArray(); gameEnd = b.checkWin(playField); return(true); } } } return(false); }
private bool checkIfBlockNeeded() { int counter = 0; int oCounter = 0, xCounter = 0; for (int i = 0; i < 3; i++) //check horizontal lines { for (int j = 0; j < 3; j++) { counter++; if (playField2d[i, j] == playerSign) { oCounter++; } if (playField2d[i, j] == botSign) { xCounter++; } } if ((oCounter == 2) && (xCounter == 0)) { for (int j = 0; j < 3; j++) { if (playField2d[i, j] != playerSign && playField2d[i, j] != botSign) { AddSign b = new AddSign(playerSign, buttonList[counter - Math.Abs(3 - j)]); lastSign = b.GetSign(); playField2d[i, j] = botSign; updateArray(); gameEnd = b.checkWin(playField); } } return(true); } oCounter = 0; xCounter = 0; } /*PRADZIA*/ counter = 0; oCounter = 0; //check vertical lines xCounter = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { counter++; if (playField2d[j, i] == playerSign) { oCounter++; } if (playField2d[j, i] == botSign) { xCounter++; } } if ((oCounter == 2) && (xCounter == 0)) { for (int j = 0; j < 3; j++) { if (playField2d[j, i] != playerSign) { AddSign b = new AddSign(playerSign, buttonList[3 * (j + 1) - Math.Abs(3 - i)]); lastSign = b.GetSign(); playField2d[j, i] = botSign; updateArray(); gameEnd = b.checkWin(playField); } } return(true); } xCounter = 0; oCounter = 0; } /*PABAIGA*/ counter = 0; xCounter = 0; oCounter = 0; digitCountInCharArray(); if (digitCountInCharArray() == 6 && checkHalfCross()) { int number = randomChoice(5); switch (number) { case 1: AddSign b2 = new AddSign(playerSign, buttonList[1]); lastSign = b2.GetSign(); playField[1] = b2.GetSign(); return(true); break; case 2: AddSign b4 = new AddSign(playerSign, buttonList[3]); lastSign = b4.GetSign(); playField[3] = b4.GetSign(); return(true); break; case 3: AddSign b6 = new AddSign(playerSign, buttonList[5]); lastSign = b6.GetSign(); playField[5] = b6.GetSign(); return(true); break; case 4: AddSign b8 = new AddSign(playerSign, buttonList[7]); lastSign = b8.GetSign(); playField[7] = b8.GetSign(); return(true); break; } } counter = 0; xCounter = 0; //istrizai oCounter = 0; for (int i = 0; i < 3; i++) /* 1 5 9 buttons */ { if (playField2d[i, i] == playerSign) { oCounter++; } if (playField2d[i, i] == botSign) { xCounter++; } } if (oCounter == 2 && xCounter == 0) { for (int i = 0; i < 3; i++) { if (playField2d[i, i] != playerSign) { AddSign b = new AddSign(playerSign, buttonList[(i + 1) * 3 - Math.Abs(3 - i)]); lastSign = b.GetSign(); playField2d[i, i] = botSign; updateArray(); gameEnd = b.checkWin(playField); return(true); } } } counter = 0; xCounter = 0; oCounter = 0; for (int i = 0; i < 3; i++) /* 3 5 7 buttons */ { if (playField2d[i, 2 - i] == playerSign) { oCounter++; } if (playField2d[i, 2 - i] == botSign) { xCounter++; } } if ((xCounter == 0) && (oCounter == 2)) { for (int i = 0; i < 3; i++) { if (playField2d[i, 2 - i] != playerSign) { AddSign b = new AddSign(playerSign, buttonList[(i * 3 + (3 - i)) - 1]); lastSign = b.GetSign(); playField2d[i, 2 - i] = botSign; updateArray(); gameEnd = b.checkWin(playField); return(true); } } } return(false); }