     .  2:  Visual C#   (1)
  


                ()  -     ,   ,        ,   , , , .        ,     ,    ,    .





     .

 2:  Visual C#   (1)



  



  ,2022



ISBN978-5-0056-8043-3 (. 2)

ISBN978-5-0056-6546-1

     Ridero








                        ,     ,       Visual C#       Microsoft ().

  ()    ,    ,  ,        .    , ,    ,   ,   -   .  ,       .         .     , ,    ,   ,      ,       .           .

  (, , ) Visual Studio (  VS)   -             ,         .  ,   VS     ,       ,      (   )          Visual C#, Visual Basic Visual C++.      (   )   ,   ,          .

      ,     .         Visual C#      VC#.    ,       (      ZharkovPress.ru    )   ,       .         ,           VC#,    ;       ,         Visual C#   . ʠ  ,      ,    ,   (   ,   ,      ),     ,    .        ,         .    .          (      ,   )   (: ,  )         VS. Ƞ           (   )       .              (   Help Visual Studio,  ,     ),     , ,    (   )  , , , , ,     (  )    .          VS  ( ),          ,     .

     .

     ,    Microsoft,     Microsof.                ,     .     ,       (     )  ,           ,            ,           () .

                   ,   ( )    Visual C#      ,                  .     ,   ,    , , , ,   .

    (     )       .

,       email:

valery-zharkov@mtu-net.ru

     :

www.ZharkovPress.ru.

          ( ).



 III.      ⠫-     



 11.      ⠫- 3x3




11.1.  


       ,      :  1  2ꠖ .     ,    [Philip Conrod, Lou Tylee. Programming Games with Visual C#. Publisher: Kidware Software LLC; 15edition (11July 2017)],     VS.

     .       Label.           .    :

1)  ,     Form1.cs [Design]*;

2)   (user interface UI),     Form1.cs;

3)   (artificial intellect AI)   ComputerTurn (),    Form1.cs.

      ꠖ  (     )     .

     1  2(  )       (      2).




11.2. 


1.        ( F5  Ctrl+F5).     ,         (    ,   ,       ,   ).  ࠖ      (  ).       ,     (  X)   (. 11.1).

2.   Start Game,   .    (   Stop Game)   ,  Exit   (. 11.2).  Label   ,   X. X      (  ,   ,  ).








. 11.1.








. 11.2.



       .     ,   .        .    ,   ,       Stop Game.       ,   ,  ,  .

3.   ();   ,      (O)    (. 11.2).

4.     ,     (. 11.3).








. 11.3.



5.       X    .         (. 11.4).

,    .

6.   ,  X   .

7.   ,  Π   (. 11.5).

8.   X  ,    (. 11.6).

,   ,   .

             .

        () .

9.      (    1  2),    Two Players.   1   2  .    Random Computer  Two Players,        (. 11.7).

10.  Exit   .








. 11.4.








. 11.5.








. 11.6.








. 11.7.

      ,     ,       .




11.3.  


   : VS  New Project  Project types    Visual C#, Windows,  Templates   Templates, Visual C#, Windows Classic Desktop, Windows Forms App (.NET Framework),  Name    , , TicTacToe_variant1  OK.  ,   Form1   .

C   Toolbox     Control       :

		Form1 Form:
		Property Name Property Value
		Name frmTicTacToe
		BackColor White
		Text TicTacToe_variant1
		FormBorderStyle Fixed Single
		StartPosition CenterScreen
		Size 478;410

		label1 Label:
		Property Name Property Value
		Name lblMessage
		AutoSize False
		BackColor Yellow
		ForeColorBlue
		BorderStyle Fixed3D
		Font StyleBold
		Font Size24
		Text XsMove
		TextAlign MiddleCenter
		Location 12;9
		Size 272;46

		label2 Label:
		Property Name Property Value
		Name lblBox1
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 12;66
		Size 76;76

		label3 Label:
		Property Name Property Value
		Name lblBox2
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 110;66
		Size 76;76

		label4 Label:
		Property Name Property Value
		Name lblBox3
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 208;66
		Size 76;76

		label5 Label:
		Property Name Property Value
		Name lblBox4
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 12;177
		Size 76;76

		label6 Label:
		Property Name Property Value
		Name lblBox5
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 110;177
		Size 76;76

		label7 Label:
		Property Name Property Value
		Name lblBox6
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 208;177
		Size 76;76

		label8 Label:
		Property Name Property Value
		Name lblBox7
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 12;287
		Size 76;76

		label9 Label:
		Property Name Property Value
		Name lblBox8
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 110;287
		Size 76;76

		label10 Label:
		Property Name Property Value
		Name lblBox9
		Font Microsoft Sans Serif;48pt
		AutoSize False
		BorderStyleNone
		Font Size48
		TextX
		TextAlign MiddleCenter
		Location 208;287
		Size 76;76

		label11 Label (used as grid line make very skinny):
		Property Name Property Value
		Name label11
		AutoSize False
		BackColorBlue
		BorderStyleNone
		Text [blank]
		Location 12;151
		Size 272;13

		label12 Label (used as grid line make very skinny):
		Property Name Property Value
		Name label12
		AutoSize False
		BackColorBlue
		BorderStyleNone
		Text [blank]
		Location 12;263
		Size 272;13

		label13 Label (used as grid line make very skinny):
		Property Name Property Value
		Name label13
		AutoSize False
		BackColorBlue
		BorderStyleNone
		Text [blank]
		Location 94;66
		Size 10;298

		label14 Label (used as grid line make very skinny):
		Property Name Property Value
		Name label14
		AutoSize False
		BackColorBlue
		BorderStyleNone
		Text [blank]
		Location 192;66
		Size 10;298

		groupBox1 GroupBox:
		Property Name Property Value
		Name grpPlayers
		AutoSize False
		BackColorBlue
		Text [blank]
		Location 295;12
		Size 156;74

		radioButton1 Radio Button:
		Property Name Property Value
		Name rdoTwoPlayers
		AutoSize False
		ForeColor White
		Font Size9
		Text Two Players
		CheckedTrue
		Location 13;19
		Size 91;19

		radioButton2 Radio Button:
		Property Name Property Value
		Name rdoOnePlayer
		AutoSize False
		ForeColor White
		Font Size9
		Text Player Computer
		Location 13;44
		Size 137;19

		groupBox2 GroupBox:
		Property Name Property Value
		Name grpFirst
		AutoSize False
		BackColorBlue
		Text [blank]
		Location 295;92
		Size 156;74

		radioButton3 Radio Button:
		Property Name Property Value
		Name rdoYouFirst
		AutoSize False
		ForeColor White
		Font Size9
		Text You Go First
		CheckedTrue
		Location 13;19
		Size 91;19

		radioButton4 Radio Button:
		Property Name Property Value
		Name rdoComputerFirst
		AutoSize False
		ForeColor White
		Font Size9
		Text Computer Goes First
		Location 13;44
		Size 137;19

		groupBox3 GroupBox:
		Property Name Property Value
		Name grpComputer
		AutoSize False
		BackColorBlue
		Text [blank]
		Location 295;172
		Size 156;74

		radioButton5 Radio Button:
		Property Name Property Value
		Name rdoRandom
		AutoSize False
		ForeColor White
		Font Size9
		Text Random Computer
		CheckedTrue
		Location 13;19
		Size 130;19

		radioButton6 Radio Button:
		Property Name Property Value
		Name rdoSmart
		AutoSize False
		ForeColor White
		Font Size9
		Text Smart Computer
		Location 13;44
		Size 115;19

		button1 Button:
		Property Name Property Value
		Name btnStartStop
		AutoSize False
		Font Size10
		Text StartGame
		Location 308;263
		Size 123;36

		button2 Button:
		Property Name Property Value
		Name btnExit
		AutoSize False
		Font Size10
		TextExit
		Location 308;305
		Size 123;36

  ,  . 11.8.








. 11.8.  Form1  .



   lblBox1,  Properties     Events   Click,       lblBox1_Click (. 11.9).

      lblBox2, lblBox3, lblBox4, lblBox5, lblBox6, lblBox7, lblBox8, lblBox9.













. 11.9.  lblBox1   lblBox1_Click.



. 11.12ࠖ    Tic Tac Toe.   Label   .   Label ,     .     X O ( ,   X    ).    Label    - .     GroupBox  Button.  GroupBox       RadioButton,     .     Button ,      .

    ,       , , Sounds,    .     beep. wav tada. wav  :     ,    Add, Existing Item,  Add Existing Item  Files oftype  All Files,    ( , , )   Ctrl      Add.   Solution Explorer     (. 11.10).

     Solution Explorer,    ,  .

    , ,       (,    MenuStrip)     ,    ZharkovPress.ru.













. 11.10.  Solution Explorer () Properties ().




11.4.   


  Form1.cs (, : File, Open, File)  Form1    .



 11.1.  .

		Random myRandom = new Random();
		Label [] boxArray = new Label[9];
		bool xTurn;
		bool canClick = false;
		int numberClicks;
		String [] possibleWins = new String[8];
		bool gameOver;
		//System.Media.SoundPlayer drawSound;
		//System.Media.SoundPlayer winSound;

		private void lblBox_Click (object sender, EventArgse)
		{
		String whoWon =;
		inti;
		if (canClick)
		{
		// see which box is clicked
		Label clickedBox;
		clickedBox = (Label) sender;
		// last digit ofname (-1) is index
		i= Convert.ToInt32(clickedBox.Name[
		clickedBox.Name. Length 1].ToString ())1;
		// if already clicked thenexit
		if (boxArray [i].Text!=)
		return;
		numberClicks++;
		if (xTurn)
		{
		boxArray [i].Text =X;
		xTurn = false;
		lblMessage. Text = Os Turn;
		}
		else
		{
		boxArray [i].Text =O;
		xTurn = true;
		lblMessage. Text = Xs Turn;
		}
		// check for win will establish avalue for WhoWon
		whoWon = CheckForWin();
		if (whoWon!=)
		{
		//winSound.Play();
		lblMessage. Text = whoWon + " wins!;
		gameOver = true;
		btnStartStop.PerformClick();
		return;
		}
		else if (numberClicks ==9)
		{
		//draw
		//drawSound.Play();
		lblMessage. Text = Its adraw!;
		gameOver = true;
		btnStartStop.PerformClick();
		return;
		}
		if (rdoOnePlayer.Checked)
		if ((xTurn && rdoComputerFirst.Checked)||
		(!xTurn && rdoYouFirst.Checked))
		ComputerTurn();
		}
		}
		private String CheckForWin()
		{
		String winner =;
		int [] boxNumber = new int[3];
		String [] mark = new String[3];
		// check all possible forwins
		for (int i= 0; i<8;i++)
		{
		for (int j = 0; j <3;j++)
		{
		boxNumber [j] = Convert.ToInt32(
		possibleWins[i][j].ToString());
		mark [j] = boxArray [boxNumber [j]].Text;
		}
		if (mark [0] == mark [1] && mark [0] == mark [2]&&
		mark [1]==
		mark [2] && mark [0]!=)
		{
		// we have awinner
		winner = mark[0];
		for (int j = 0; j <3;j++)
		boxArray[boxNumber[j]].BackColor=
		Color.Red;
		}
		}
		return (winner);
		}
		private void ComputerTurn()
		{
		int selectedBox;
		int i,n;
		int j,k;
		String computerMark, playerMark, markToFind;
		int [] boxNumber = new int[3];
		String [] mark = new String[3];
		int emptyBox;
		int [] bestMoves = {4, 0, 2, 6, 8, 1, 3, 5,7};
		if (rdoRandom.Checked)
		{
		// random logic
		// put mark inNth available square
		n = myRandom.Next (9 numberClicks)+1;
		i=0;
		for (selectedBox = 0; selectedBox<9;
		selectedBox++)
		{
		if (boxArray [selectedBox].Text ==)
		i++;
		if (i==n)
		break;
		}
		// put mark inSelectedBox
		lblBox_Click (boxArray [selectedBox], null);
		}
		else
		{
		// smart computer
		// determine who has whatmark
		if (rdoComputerFirst.Checked)
		{
		computerMark =X;
		playerMark =O;
		}
		else
		{
		computerMark =O;
		playerMark =X;
		}
		// Step 1(K = 1) check for win
		// see if two boxeshold
		// computer mark and one is empty
		// Step 2(K = 2) check for block
		// see if two boxeshold
		// player mark and one is empty
		for (k = 1; k <= 2;k++)
		{
		if (k ==1)
		markToFind = computerMark;
		else
		markToFind = playerMark;
		for (i= 0; i<8;i++)
		{
		n =0;
		emptyBox =0;
		for (j = 0; j <3;j++)
		{
		boxNumber [j] = Convert.ToInt32(
		possibleWins[i][j].ToString());
		mark [j] = boxArray [boxNumber [j]].Text;
		if (mark [j] == markToFind)
		n++;
		else if (mark [j] ==)
		emptyBox = boxNumber[j];
		}
		if (n == 2&& emptyBox!=0)
		{
		// mark empty box towin (K = 1)or
		// block (K =2)
		lblBox_Click (boxArray [emptyBox], null);
		return;
		}
		}
		}
		// Step 3 find next bestmove
		for (i= 0; i<9;i++)
		{
		if (boxArray [bestMoves [i]].Text ==)
		{
		lblBox_Click (boxArray [bestMoves [i]], null);
		return;

}

}

}

}



       Properties ( )  Events     Load.         .



 11.2. .

		private void frmTicTacToe_Load (object sender, EventArgse)
		{
		//establish array
		boxArray [0] = lblBox1;
		boxArray [1] = lblBox2;
		boxArray [2] = lblBox3;
		boxArray [3] = lblBox4;
		boxArray [4] = lblBox5;
		boxArray [5] = lblBox6;
		boxArray [6] = lblBox7;
		boxArray [7] = lblBox8;
		boxArray [8] = lblBox9;
		// possiblewins
		possibleWins [0] = 012;
		possibleWins [1] = 345;
		possibleWins [2] = 678;
		possibleWins [3] = 036;
		possibleWins [4] = 147;
		possibleWins [5] = 258;
		possibleWins [6] = 048;
		possibleWins [7] = 246;
		// clear boxes
		for (int i= 0; i<9;i++)
		boxArray [i].Text =;
		lblMessage. Text = Game Stopped;
		grpFirst. Enabled = false;
		grpComputer. Enabled = false;
		//drawSound = new System.Media.SoundPlayer(
		//Application.StartupPath + "\\beep. wav);
		//winSound = new System.Media.SoundPlayer(
		//Application.StartupPath + "\\tada. wav);

}



        Properties (  )  Events     Click.         .



 11.3. .

		private void btnStartStop_Click (object sender, EventArgse)
		{
		if (btnStartStop. Text == Start Game)
		{
		btnStartStop. Text = Stop Game;
		grpPlayers. Enabled = false;
		grpFirst. Enabled = false;
		grpComputer. Enabled = false;
		btnExit. Enabled = false;
		xTurn = true;
		lblMessage. Text = Xs Turn;
		// reset boxes
		for (int i= 0; i<9;i++)
		{
		boxArray [i].Text =;
		boxArray[i].BackColor = Color. White;
		}
		canClick = true;
		numberClicks =0;
		gameOver = false;
		if (rdoComputerFirst.Checked&&
		rdoOnePlayer.Checked)
		ComputerTurn();
		}
		else
		{
		btnStartStop. Text = Start Game;
		if (!gameOver)
		lblMessage. Text = Game Stopped;
		grpPlayers. Enabled = true;
		if (rdoOnePlayer.Checked)
		{
		grpFirst. Enabled = true;
		grpComputer. Enabled = true;
		}
		btnExit. Enabled = true;
		canClick = false;

}

}



        Properties (  )  Events     Click.         .



 11.4. .



private void btnExit_Click (object sender, EventArgse)

{

this.Close();

}



  Properties     Events     Click.         .



 11.5. .

		private void rdoOnePlayer_Click (object sender, EventArgse)
		{
		grpFirst. Enabled = true;
		grpComputer. Enabled = true;

}



  Properties     Events     Click.         .



 11.6. .

		private void rdoTwoPlayers_Click (object sender, EventArgse)
		{
		grpFirst. Enabled = false;
		grpComputer. Enabled = false;

}



  ,           ,    ,    Sound,  .

     : Build, Build Selection; Debug, Start Without Debugging.

  Visual Studio   Form1     9.

      1  2-    .

        -     ,     12.



 12.      ⠫- 6x7




12.1.  


          -.       ,   .

      , , ,    , , 6x 7 (      x   y)       ,    ,         .

     ,   [.  : Article from the website: www.codeproject.com/netcf/Connect4AB.asp: Labib B. Connect4 using Alpha-Beta Search algorithm],      Visual Studio.

   ( )    .    FillRectangle  Graphics    ( ),    RoyalBlue:



e.Graphics.FillRectangle (new SolidBrush(Color.RoyalBlue),

15, 70, 210, 180);



 15, 70      (   ,   y  );

210, 180     x 蠫y.

  FillEllipse  Graphics    6x 7 ()  27   White:



g.FillEllipse (new SolidBrush (Color. White),

x +3, y +3, 27,27);



     ,      ,   Yellow:



g.FillEllipse (new SolidBrush (Color. Yellow),

x +3, y +3, 27,27);



   (  ,    )         Red:

g.FillEllipse (new SolidBrush(Color.Red),

x +3, y +3, 27,27);



       ,    ,     4-   .       ()    Beep (-: ).




12.2. 


1.      ,  7x 6  (. 12.1). ,       x 蠫y.         ,     (. 12.1).       ,      (. 12.2).













. 12.1.   . . 12.2.   (Level).



2.       ,      .          Beep.

   (  ,    )          (. 12.3).

3.        ,    ,     4-   .     ,        4-   ,   ( )      (    ).    . 12.4.

4.         4-   ,    Show  MessageBox   ,    (. 12.5),    (. 12.6).

   OK.

5.             (  ).

6.        Close.













. 12.3.  . . 12.4.  .













. 12.5.  . . 12.6.  .



      ,      ,       .




12.3.  


   : VS  New Project ( )   Templates   ( ) Visual C#, Windows Classic Desktop ,   Templates   ( , )  Windows Forms App (.NET Framework);  Name  (  )  , , Connect4 (. 12.7, 12.8, 12.9) OK.


















. 12.7.  Form1. . 12.8.  Level. . 12.9. SE Properties.



 ,   Form1    (. 12.7).

    ,        ZharkovPress.ru    .

   ,  ,   Toolbox     MenuStrip (. 12.7).

 Form1    Type Here ( ),   , : New game ( ), Begins the player ( ), Begins the computer ( ), : Level (), Usual (), Normal (), Difficult (), . 12.8.       ,    Properties (     MenuStrip)  Name       , ,   : newGame, playerFirst, computerFirst,   : level, easy, normal, hard.

      Beep    - , ,     ,    . 1- ,         ()   ,       , , Visual Basic.    Project   Add Reference,  Reference Manager       (dynamic link library)  (.dll),  ,  Microsoft.VisualBasic   OK.   ,     , , - Form1_MouseDown    , ,   ,   :



Microsoft.VisualBasic.Interaction.Beep();



2- ,   ,      Beep, ,      Enter,   :



System.Media.SystemSounds.Beep.Play();




12.4.  


  Form1.cs (, : File, Open, File)  Form1    .



 12.1.  .

		public int cx =15;
		public int cy =70;
		public int Level =3;
		public Connect4Board b = new Connect4Board();

		public void Draw (Graphicsg)
		{
		Pen p = new Pen (Color. Black);
		g. DrawRectangle (p, 15, 70, 210, 180);
		int q =0;
		int w=5;
		for (int y = cy; y <cy +180; y +=30)
		{
		for (int x = cx; x <cx +210; x +=30)
		{
		if (b.arr [q, w] ==0)
		g.FillEllipse (new SolidBrush (Color. White),
		x +3, y +3, 27,27);
		if (b.arr [q, w] ==1)
		g.FillEllipse (new SolidBrush (Color. Yellow),
		x +3, y +3, 27,27);
		if (b.arr [q, w] ==2)
		g.FillEllipse (new SolidBrush(Color.Red),
		x +3, y +3, 27,27);
		g. DrawEllipse (p, x +3, y +3, 27,27);
		q++;
		}
		q =0;
		w;

}

}



  Properties ( Form1)  Events     Paint.    Form1_Paint       . ,     , ,   PictureBox   -     .



 12.2.    .



private void Form1_Paint (object sender, PaintEventArgse)

{

e.Graphics.FillRectangle (new SolidBrush(Color.RoyalBlue),

15, 70, 210, 180);

Draw(e.Graphics);


}



  Properties ( Form1)  Events     MouseDown.    Form1_MouseDown      .



 12.3.      .

		private void Form1_MouseDown (object sender, MouseEventArgse)
		{
		Microsoft.VisualBasic.Interaction.Beep();
		if (b. endt ==0)
		{
		if ((e.X> cx) && (e.X <cx +210))
		{
		if ((e.Y> cy) && (e.Y <cy +180))
		{
		int selection = (e.X cx) /30;
		if ((selection> =0) && (selection<7))
		{
		if (b.add (selection, b.plr) ==0)
		{
		Draw(this.CreateGraphics());
		b. turncheck();
		b. turn = b.cpu;
		if (b. endt ==0)
		{
		intck;
		b.col = b.Think();
		ck = b.add(b.col, b. cpu);
		if (ck ==0)
		{
		b.lin = 6 b.tops[b.col];
		b. turn = b.plr;
		}
		}
		Draw(this.CreateGraphics());
		b. turncheck();

}

}

}

}

}

}



   Begins the player ( )    MenuStrip (  Properties  Events   Click).   ,       .



 12.4. -  .

		private void playerFirstToolStripMenuItem_Click(
		object sender, EventArgse)
		{
		//Player First
		b = new Connect4Board();
		b.rec = Level;
		Invalidate();

}



   Begins the computer ( )    MenuStrip.   ,       .



 12.5. -  .

		private void computerFirstToolStripMenuItem_Click(
		object sender, EventArgse)
		{
		//CPU First
		b = new Connect4Board();
		b.rec = Level;
		Draw(this.CreateGraphics());
		b. turn = b.cpu;
		if (b. endt ==0)
		{
		intck;
		b.col = b.Think();
		ck = b.add(b.col, b. cpu);
		if (ck ==0)
		{
		b.lin = 6 b.tops[b.col];
		b. turn = b.plr;
		}
		}
		Draw(this.CreateGraphics());
		b. turncheck();

}



   Usual ().   ,       .



 12.6. -  .

		private void easyToolStripMenuItem_Click(
		object sender, EventArgse)
		{
		easyToolStripMenuItem.Checked = true;
		hardToolStripMenuItem.Checked = false;
		normalToolStripMenuItem.Checked = false;
		//Easy
		Level =3;
		b = new Connect4Board();
		b.rec = Level;
		Invalidate();

}



   Normal ().   ,       .



 12.7. -  .

		private void normalToolStripMenuItem_Click(
		object sender, EventArgse)
		{
		hardToolStripMenuItem.Checked = false;
		easyToolStripMenuItem.Checked = false;
		normalToolStripMenuItem.Checked = true;
		//Normal
		Level =5;
		b = new Connect4Board();
		b.rec = Level;
		Invalidate();

}



   Difficult ().   ,       .



 12.8. -  .

		private void hardToolStripMenuItem_Click(
		object sender, EventArgse)
		{
		hardToolStripMenuItem.Checked = true;
		easyToolStripMenuItem.Checked = false;
		normalToolStripMenuItem.Checked = false;
		//Hard
		Level =7;
		b = new Connect4Board();
		b.rec = Level;
		Invalidate();

}



    , ,        (   MenuStrip)      ZharkovPress.ru      .

      Form1 (  Form1   ).

      (    ).

     .

 ,      :  Solution Explorer     ,    Add, Existing Item,  Add Existing Item  Files oftype  All Files,    (,   ,  ),       Add (     ).

 ,  Solution Explorer         Add, New Item,  Add New Item   Code File,  Name   Connect4Board. cs   Add.   ( Solution Explorer)   ,     ,     .



 12.9.  .

		using System;
		using System. Drawing;
		using System.Collections;
		using System.Windows.Forms;

		namespace Connect4
		{
		public class Connect4Board
		{
		public Random random = new Random();
		public int [,] arr = new int [7,6];
		public int [,] thn = new int [7,6];
		public int [] tops = new int[7];
		public int player, computer, endt =0;
		public int plr = 1, cpu = 2, rec = 3, turn =1;
		public int m, n, r, temp, so, ch, col, t,y;
		public int plrcoin, cpucoin;
		public intlin;
		public Connect4Board()
		{
		}
		public int Think()
		{
		inti;
		i=rec;
		return check (i, -9999999, 9999999);
		}
		public void turncheck()
		{
		int temp;
		char [] toto = new char [20];
		temp = checkwin();
		if (temp ==plr)
		{
		MessageBox.Show (You won!);
		endt = 1; return;
		}
		if (temp ==cpu)
		{
		MessageBox.Show (You lost.);
		endt = 1; return;
		}
		if (temp ==0)
		{
		for (t = 0; t <= 6;t++)
		if (tops [t] <6) temp =1;
		if (temp ==0)
		{
		//drawn();
		endt =1;
		return;
		}
		}
		}
		public int check (int i, int alpha, int beta)
		{
		int co, score, t, g, j = 0,p;
		i;
		if (i== -1) {score = position (); return score;}
		if (i% 2==0)
		{
		int max = 0,k;
		j = 0; co =0;
		for (t = 0; t <7;t++)
		{
		g = add (t, cpu);
		if (g ==0)
		{
		if (checkwin () ==cpu)
		{
		sub(t);
		if (i== rec1)
		returnt;
		else return 9000;
		}
		k = check (i, alpha, 999999);
		if (k> alpha) alpha =k;
		sub(t);
		if (k> beta) returnk;
		if (co == 0) {max = k; co = 1; j =t;}
		if (k ==max)
		{
		p = (random.Next (6))+1;
		if (p> 4) j =t;
		}
		if (k> max) {max = k; j =t;}
		}
		}
		score =max;
		}
		else
		{
		int min = 0, k =0;
		co =0;
		for (t = 0; t <7;t++)
		{
		g = add (t, plr);
		if (g ==0)
		{
		if (checkwin () ==plr)
		{
		sub(t);
		/*if (i==rec-1) return t; else*/
		return -10000;
		}
		k = check (i, -99999, beta);
		if (k <beta) beta =k;
		sub(t);
		if (k <alpha) returnk;
		if (co == 0) {min = k; co = 1; j =t;}
		if (k <min) {min = k; j =t;}
		}
		}
		score =min;
		}
		if (i== rec 1) returnj;
		return score;
		}
		public int add (int c, int coin)
		{
		if (tops [c]<6)
		{
		arr [c, tops [c]] = coin;
		tops [c] ++; return0;
		}
		return1;
		}
		public int sub (intc)
		{
		tops [c];
		arr [c, tops [c]] =0;
		return0;
		}
		public int position()
		{
		int u, o, x, y, j, score;
		int gh = 0, hg =0;
		score =0;
		//Empty the think array

		for (x = 0; x <7;x++)
		{
		for (y = 0; y <6;y++)
		{
		thn [x, y] =0;
		}
		}
		//Sum the score ofevery opportunity tothe score
		for (y = 0; y <6;y++)
		{
		for (x = 0; x <7;x++)
		{
		if (arr [x, y] ==0)
		score = score + checkhole (x,y);
		if (y>0)
		{
		if ((thn [x, y] == cpu)&&
		(arr [x, y 1]!= 0)) gh++;
		if ((thn [x, y] == plr)&&
		(arr [x, y 1]!=0))
		{hg++; score = score 4000;}
		}
		else
		{
		if (thn [x, y] == cpu) gh++;
		if (thn [x, y] ==plr)
		{hg++; score = score 4000;}
		}
		}
		}
		if (gh> 1) score = score + (gh 1) *500;
		if (gh == 1) score = score100;
		if (hg> 1) score = score (hg 1) *500;
		for (x = 0; x <7;x++)
		{
		gh =0;
		for (y = 1; y <6;y++)
		{
		/*if (gh==0)
		if ((thn [x,y]> 0) && (arr [x,y-1] ==0)){
		gh=1;

		}*/
		if ((thn [x, y] == cpu)&&
		(thn [x, y 1] == cpu))
		{
		u= 0; j =0;
		for (o= y 1; o> -1; o)
		{
		if (thn [x, o] == plr) u=1;
		if (arr [x, o] == 0)j++;
		}
		if (u== 0) score = score +1300 j *7;
		if (u== 1) score = score +300;
		}
		if ((thn [x, y] == plr)&&
		(thn [x, y 1] == plr))
		{
		u= 0; j =0;
		for (o= y 1; o> -1; o)
		{
		if (thn [x, o] == cpu) u=1;
		if (arr [x, o] == 0)j++;
		}
		if (u== 0) score = score 1500+ j *7;
		if (u== 1) score = score300;
		}
		if (thn [x, y] ==plr)
		{
		u=0;
		for (o= y 1; o> -1; o)
		{
		if (thn [x, o] == cpu) u=1;
		}
		if (u== 1) score = score+30;
		}
		if (thn [x, y] ==cpu)
		{
		u=0;
		for (o= y 1; o> -1; o)
		{
		if (thn [x, o] == plr) u=1;
		}
		if (u== 1) score = score30;
		}
		}
		}
		return score;
		}
		public int checkhole (int x, inty)
		{
		int score =0;
		int max,min;
		int d0 = 0, d1 = 0, d2 = 0, d3 =0;
		if (((x +1) <7) && ((y 1)>-1))
		{
		if (arr [x +1, y 1] ==cpu)
		{
		d1++;
		if (((x +2) <7) && ((y 2)>-1))
		{
		if (arr [x +2, y 2] ==cpu)
		{
		d1++;
		if (((x +3) <7) && ((y 3)>-1))
		{
		if (arr [x +3, y 3] ==cpu)
		d1++;
		}
		}
		}
		}
		}
		if (((x 1)> -1) && ((y +1)<6))
		{
		if (arr [x 1, y +1] ==cpu)
		{
		d1++;
		if (((x 2)> -1) && ((y +2)<6))
		{
		if (arr [x 2, y +2] ==cpu)
		{
		d1++;
		if (((x 3)> -1) && ((y +3)<6))
		{
		if (arr [x 3, y +3] == cpu) d1++;
		}
		}
		}
		}
		}
		if (((x 1)> -1) && ((y 1)>-1))
		{
		if (arr [x 1, y 1] ==cpu)
		{
		d2++;
		if (((x 2)> -1) && ((y 2)>-1))
		{
		if (arr [x 2, y 2] ==cpu)
		{
		d2++;
		if (((x 3)> -1) && ((y 3)>-1))
		{
		if (arr [x 3, y 3] == cpu) d2++;
		}
		}
		}
		}
		}
		if (((x +1) <7) && ((y +1)<6))
		{
		if (arr [x +1, y +1] ==cpu)
		{
		d2++;
		if (((x +2) <7) && ((y +2)<6))
		{
		if (arr [x +2, y +2] ==cpu)
		{
		d2++;
		if (((x +3) <7) && ((y +3)<6))
		{
		if (arr [x +3, y +3] == cpu) d2++;
		}
		}
		}
		}
		}
		if ((y 1)> -1) if (arr [x, y 1] ==cpu)
		{
		d0++;
		if ((y 2)> -1) if (arr [x, y 2] ==cpu)
		{
		d0++;
		if ((y 3)>-1)
		if (arr [x, y 3] == cpu) d0++;
		}
		}
		if (x 1>-1)
		{
		if (arr [x 1, y] ==cpu)
		{
		d3++;
		if (x 2>-1)
		{
		if (arr [x 2, y] ==cpu)
		{
		d3++;
		if (x 3>-1)
		if (arr [x 3, y] == cpu) d3++;
		}
		}
		}
		}
		if (x +1<7)
		{
		if (arr [x +1, y] ==cpu)
		{
		d3++;
		if (x +2<7)
		{
		if (arr [x +2, y] ==cpu)
		{
		d3++;
		if (x +3<7)
		if (arr [x +3, y] == cpu) d3++;
		}
		}
		}
		}
		max =d0;
		if (d1> max) max =d1;
		if (d2> max) max =d2;
		if (d3> max) max =d3;
		if (max == 2) score = score+5;
		if (max>2)
		{
		score = score +71; thn [x, y] =cpu;
		if ((d1 <3) && (d2 <3) && (d3<3))
		score = score10;
		}
		if (((x +1) <7) && ((y 1)>-1))
		{
		if (arr [x +1, y 1] ==plr)
		{
		d1++;
		if (((x +2) <7) && ((y 2)>-1))
		{
		if (arr [x +2, y 2] ==plr)
		{
		d1++;
		if (((x +3) <7) && ((y 3)>-1))
		{
		if (arr [x +3, y 3] == plr) d1++;
		}
		}
		}
		}
		}
		if (((x 1)> -1) && ((y +1)<6))
		{
		if (arr [x 1, y +1] ==plr)
		{
		d1++;
		if (((x 2)> -1) && ((y +2)<6))
		{
		if (arr [x 2, y +2] ==plr)
		{
		d1++;
		if (((x 3)> -1) && ((y +3)<6))
		{
		if (arr [x 3, y +3] == plr) d1++;
		}
		}
		}
		}
		}
		if (((x 1)> -1) && ((y 1)>-1))
		{
		if (arr [x 1, y 1] ==plr)
		{
		d2++;
		if (((x 2)> -1) && ((y 2)>-1))
		{
		if (arr [x 2, y 2] ==plr)
		{
		d2++;
		if (((x 3)> -1) && ((y 3)>-1))
		{
		if (arr [x 3, y 3] == plr) d2++;
		}
		}
		}
		}
		}
		if (((x +1) <7) && ((y +1)<6))
		{
		if (arr [x +1, y +1] ==plr)
		{
		d2++;
		if (((x +2) <7) && ((y +2)<6))
		{
		if (arr [x +2, y +2] ==plr)
		{
		d2++;
		if (((x +3) <7) && ((y +3)<6))
		{
		if (arr [x +3, y +3] == plr) d2++;
		}
		}
		}
		}
		}
		if ((y 1)> -1) if (arr [x, y 1] ==plr)
		{
		d0++;
		if ((y 2)> -1) if (arr [x, y 2] ==plr)
		{
		d0++;
		if ((y 3)>-1)
		if (arr [x, y 3] == plr) d0++;
		}
		}
		if (x 1>-1)
		{
		if (arr [x 1, y] ==plr)
		{
		d3++;
		if (x 2>-1)
		{
		if (arr [x 2, y] ==plr)
		{
		d3++;
		if (x 3>-1)
		if (arr [x 3, y] == plr) d3++;
		}
		}
		}
		}
		if (x +1<7)
		{
		if (arr [x +1, y] ==plr)
		{
		d3++;
		if (x +2<7)
		{
		if (arr [x +2, y] ==plr)
		{
		d3++;
		if (x +3<7)
		if (arr [x +3, y] == plr) d3++;
		}
		}
		}
		}
		min =d0;
		if (d1> min) min =d1;
		if (d2> min) min =d2;
		if (d3> min) min =d3;
		if (min == 2) score = score4;
		if (min>2)
		{
		score = score 70; thn [x, y] =plr;
		if ((d1 <3) && (d2 <3) && (d3<3))
		score = score+10;
		}
		return score;
		}
		public int checkwin()
		{
		int r, x,y;
		r =0;
		for (y = 2; y> -1; y)
		{
		for (x = 0; x <7;x++)
		{
		checku (x, y, refr);
		}
		}
		for (y = 0; y <6;y++)
		{
		for (x = 0; x <4;x++)
		{
		check2r (x, y, refr);
		}
		}
		for (y = 2; y> -1; y)
		{
		for (x = 0; x <4;x++)
		{
		checkr (x, y, refr);
		}
		}
		for (y = 2; y> -1; y)
		{
		for (x = 3; x <7;x++)
		{
		checkl (x, y, refr);
		}
		}
		returnr;
		}
		public void checku (int x, int y, ref intr)
		{
		if ((arr [x, y] == 2) && (arr [x, y +1] == 2)&&
		(arr [x, y +2] == 2)&&
		(arr [x, y +3] == 2)) r =2;
		if ((arr [x, y] == 1) && (arr [x, y +1] == 1)&&
		(arr [x, y +2] == 1)&&
		(arr [x, y +3] == 1)) r =1;
		}
		public void check2r (int x, int y, ref intr)
		{
		if ((arr [x, y] == 2) && (arr [x +1, y] == 2)&&
		(arr [x +2, y] == 2)&&
		(arr [x +3, y] == 2)) r =2;
		if ((arr [x, y] == 1) && (arr [x +1, y] == 1)&&
		(arr [x +2, y] == 1)&&
		(arr [x +3, y] == 1)) r =1;
		}
		public void checkr (int x, int y, ref intr)
		{
		if ((arr [x, y] == 2) && (arr [x +1, y +1] == 2)&&
		(arr [x +2, y +2] == 2)&&
		(arr [x +3, y +3] == 2)) r =2;
		if ((arr [x, y] == 1) && (arr [x +1, y +1] == 1)&&
		(arr [x +2, y +2] == 1)&&
		(arr [x +3, y +3] == 1)) r =1;
		}
		public void checkl (int x, int y, ref intr)
		{
		if ((arr [x, y] == 2) && (arr [x 1, y +1] == 2)&&
		(arr [x 2, y +2] == 2)&&
		(arr [x 3, y +3] == 2)) r =2;
		if ((arr [x, y] == 1) && (arr [x 1, y +1] == 1)&&
		(arr [x 2, y +2] == 1) &&(
		arr [x 3, y +3] == 1)) r =1;

}

}

}



    Solution Explorer   ,   .   SE  ,    ,  .




12.5. 


     :

Build, Build Selection; Debug, Start Without Debugging.

  Visual C#   Form1      ().

    -    .

        -,    .



 IV.               



 13.           




13.1.  


           .    ( ZharkovPress.ru)       ,             Toolbox,   [.  : Game Tiny Tennis from the website ofMicrosoft corporation],      Visual Studio.

,    .      Form1,   (,   )    1, ࠖ   2(  )     2(  ).      4-  ,       .       1,         2   2.        , ,        ,      ,         ,      Random.

     2(  ,  )               ,         ,       (  )  1 1.    2  ()     ,     1.

   1        ,       (  )    2 1 (  ).

 ,       ( 1    2), - Form1  :



_player2 = new Bat (pictureBox2, ClientSize. Width 30

Bat. Width, Keys. Up, Keys.Down, 0, ClientSize. Height);



,   ( 2)       Keys. Up, 砖 Keys.Down.

   ( 1    2), - Form1      :



_player2 = new Bat (pictureBox2, ClientSize. Width 30

Bat. Width, 0, ClientSize. Height);



  (  2)            ,       .

  ,          .

            ,      .




13.2. 


1.     Form1,        1, ࠖ   2(  )     2(  ).       (    Label);    0:0, . 13.1.

       PictureBox.

     4-  ,     .           beep. wav.       1,       2(   2).








. 13.1.    (ࠖ   1, ࠖ  2).



2.     2(  ,  )           ,         ,       (  )  1 1  . Ƞ 2,    2     ,     1.

   1        ,       (  )    2(    2)  1 .

. 13.2  4:11 2.








. 13.2.  ,  4:11(ࠖ   1, ࠖ  2).



3.  ,          .  ,  (  2) .

4.     ,    ,       .

5.    ,    Close.

      ,     ,      .

, ,                (     ).




13.3.  


   : VS  New Project ( )   Templates   ( ) Visual C#, Windows Classic Desktop ,   Templates   ( , )  Windows Forms App (.NET Framework);  Name  (  )  , , TinyTennis OK.

 ,   Form1   (. 13.3. 13.4).     ,            ZharkovPress.ru.      ,   Properties  Size , ,  : 800;559.




  .


   .

   ,     (https://www.litres.ru/pages/biblio_book/?art=67920189)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


