Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //link https://yadi.sk/d/xZkdriK5gUHufw
- #include <FL/Fl.H>
- #include <FL/Fl_Window.H>
- #include <FL/Fl_Button.H>
- #include <FL/Fl_Input.H>
- #include <FL/Fl_Output.H>
- #include <FL/fl_draw.H>
- #include <FL/Fl_File_Chooser.H>
- #include <iostream>
- #include <string>
- Fl_Input* inp;
- Fl_Button* but[9][9];
- Fl_Button* first;
- Fl_Window* window2;
- Fl_Window* window;
- bool b = false;
- int K = 0;
- int q, w;
- int k1 = 1;
- const char player1[32] = "Победил игрок 1 (X)";
- const char player2[32] = "Победил игрок 2 (O)";
- std::string c;
- int p;
- void recolor(int i, int j, std::string c, int p) {
- but[i][j]->color(FL_RED);
- but[i][j]->redraw();
- if (p == 1) {
- k1 = 1;
- while ((but[i + k1][j]->label()) == c) {
- but[i + k1][j]->color(FL_RED);
- but[i + k1][j]->redraw();
- k1++;
- if (i + k1 > K - 1)
- break;
- }
- k1 = 1;
- while ((but[i - k1][j]->label()) == c) {
- but[i - k1][j]->color(FL_RED);
- but[i - k1][j]->redraw();
- k1++;
- if (i - k1 < 0)
- break;
- }
- }
- else if (p == 2)
- {
- k1 = 1;
- while ((but[i][j + k1]->label()) == c) {
- but[i][j + k1]->color(FL_RED);
- but[i][j + k1]->redraw();
- k1++;
- if (j + k1 > K - 1)
- break;
- }
- k1 = 1;
- while ((but[i][j - k1]->label()) == c) {
- but[i][j - k1]->color(FL_RED);
- but[i][j - k1]->redraw();
- k1++;
- if (j - k1 < 0)
- break;
- }
- }
- else if (p == 3)
- {
- k1 = 1;
- while ((but[i + k1][j + k1]->label()) == c) {
- but[i + k1][j + k1]->color(FL_RED);
- but[i + k1][j + k1]->redraw();
- k1++;
- if (i + k1 > K - 1)
- break;
- if (j + k1 > K - 1)
- break;
- }
- k1 = 1;
- while ((but[i - k1][j - k1]->label()) == c) {
- but[i - k1][j - k1]->color(FL_RED);
- but[i - k1][j - k1]->redraw();
- k1++;
- if (i - k1 < 0)
- break;
- if (j - k1 < 0)
- break;
- }
- }
- else if (p == 4)
- {
- k1 = 1;
- while ((but[i + k1][j - k1]->label()) == c) {
- but[i + k1][j - k1]->color(FL_RED);
- but[i + k1][j - k1]->redraw();
- k1++;
- if (i + k1 > K - 1)
- break;
- if (j - k1 < 0)
- break;
- }
- k1 = 1;
- while ((but[i - k1][j + k1]->label()) == c) {
- but[i - k1][j + k1]->color(FL_RED);
- but[i - k1][j + k1]->redraw();
- k1++;
- if (i - k1 < 0)
- break;
- if (j + k1 > K - 1)
- break;
- }
- }
- if (c == "X")
- fl_alert(player1);
- else
- fl_alert(player2);
- window2->hide();
- exit(0);
- }
- void
- button_callback(Fl_Widget* o, void*)
- {
- Fl_Button* button = (Fl_Button*)o;
- if (b == false && button->label() == "") {
- button->label("X");
- b = true;
- }
- else if (b == true && button->label() == "")
- {
- button->label("O");
- b = false;
- }
- for (int i = 0; i < K; i++) {
- for (int j = 0; j < K; j++) {
- if ((i > 0) && (i < K - 1) && ((but[i][j]->label()) == (but[i + 1][j]->label())) && ((but[i][j]->label()) == (but[i - 1][j]->label())) && (but[i][j]->label() != "")) {
- p = 1;
- if (but[i][j]->label() == "X")
- recolor(i, j, "X", p);
- else
- recolor(i, j, "O", p);
- }
- if ((j > 0) && (j < K - 1) && ((but[i][j]->label()) == (but[i][j + 1]->label())) && ((but[i][j]->label()) == (but[i][j - 1]->label())) && (but[i][j]->label() != "")) {
- p = 2;
- if (but[i][j]->label() == "X")
- recolor(i, j, "X", p);
- else
- recolor(i, j, "O", p);
- }
- if ((j > 0) && (j < K - 1) && (i > 0) && (i < K - 1) && ((but[i][j]->label()) == (but[i + 1][j + 1]->label())) && ((but[i][j]->label()) == (but[i - 1][j - 1]->label())) && (but[i][j]->label() != "")) {
- p = 3;
- if (but[i][j]->label() == "X")
- recolor(i, j, "X", p);
- else
- recolor(i, j, "O", p);
- }
- if ((j > 0) && (j < K - 1) && (i > 0) && (i < K - 1) && ((but[i][j]->label()) == (but[i + 1][j - 1]->label())) && ((but[i][j]->label()) == (but[i - 1][j + 1]->label())) && (but[i][j]->label() != "")) {
- p = 4;
- if (but[i][j]->label() == "X")
- recolor(i, j, "X", p);
- else
- recolor(i, j, "O", p);
- }
- }
- }
- }
- void
- button_callback2(Fl_Widget* o, void*)
- {
- K = atoi(inp->value());
- if (K > 2 && K < 10) {
- window2 = new Fl_Window(50 * K + 100, 50 * K + 100, "Игра 'Крестики-нолики'");
- window2->color(FL_GRAY);
- window->hide();
- window2->begin();
- int n = 0;
- int nn = 0;
- for (int i = 0; i < K; i++) {
- nn = 0;
- n = n + 50;
- for (int j = 0; j < K; j++) {
- nn = nn + 50;
- but[i][j] = new Fl_Button(nn, n, 50, 50, "");
- but[i][j]->labelsize(36);
- but[i][j]->color(FL_WHITE);
- }
- }
- window2->end();
- window2->show();
- for (int v = 0; v < K; v++) {
- for (int m = 0; m < K; m++) {
- but[v][m]->callback(button_callback);
- }
- }
- }
- }
- int
- main()
- {
- window = new Fl_Window(300, 100, "Игра 'Крестики-нолики'");
- window->begin();
- first = new Fl_Button(100, 55, 100, 40, "N");
- inp = new Fl_Input(50, 5, 200, 40);
- window->end();
- first->callback(button_callback2);
- window->show();
- return Fl::run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement