تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[حصرياً] برمجة لعبة x-o باستخدام c#.net
#1
بسم الله الرحمن الرحيم

درس

أولاً : التصميم

نفتح برنامج Microsoft Visual Studio أنا استخدم Visual Studio 2010
و ننشى مشروع جديد بعنوان : Tic Tac Toe كما موضح بالصورة



و اجعل خصائص الفورم كما يلي



و الآن اضف الأدوات الموجودة في الصورة على الفورم



و اجعل خصائصها كما في الجدول



احفظ الصورة التالية و ضعها في اداة الصورة في الفورم * موجودة في ملف المشروع



و الآن ننتقل إلى اهم جزء و هو الأكواد

هذا هو الكود كامل مع الشرح

PHP كود :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 
WindowsFormsApplication1
{
    public 
partial class Form1 Form
    
{
        public 
Form1()
        {
            
InitializeComponent();
        }
        
//المعرفات الاساسية
        
private System.Windows.Forms.Label[] _sq;
        public 
string playerturn;
        public 
int turn_num;
        private 
bool _isGameOver;
        
//هذا اهم امر في المشروع و هو الذي يحدد دور اللاعب
        
public void startgame()
        {
            
turn_num 0;
            if (
xplayer.Checked == true)
            {
                
playerturn "x";
            }
            else
            {
                
playerturn "o";
            }
            for (
int i 09i++)
            {
                
_sq[i].Text "";
                
_sq[i].ForeColor Color.Black;
                
_sq[i].BackColor Color.Transparent;
                
_sq[i].Enabled true;
            }
        }
 

        
// هذا حدث ترك الماوس لكافة المربعات النصية
        
private void MouseLeaveHandler(object senderEventArgs e)
        {
            
Label tempButton = (Label)sender;
            if (
tempButton.Text != "")
            {
                return; 
            }
            else
            {
               
tempButton.BackColor Color.Transparent;  
            }
          
        }
        
// هذا حدث تحريك الماوس لكافة المربعات النصية
        
private void MouseMoveHandler(object senderMouseEventArgs e)
        {
            
Label tempButton = (Label)sender;
            if (
tempButton.Text != "")
            {
                return;
            }
            if (
playerturn == "x")
            {
                
tempButton.BackColor Color.SkyBlue;
            }
            else
            {
                
tempButton.BackColor Color.Coral;
            }
        }
        
//زر لعبة جديدة
        
private void btnnew_Click(object senderEventArgs e)
        {
            
startgame();
            for (
int i 09i++)
            {
                
this._sq[i].Click += new System.EventHandler(this.ClickHandler);
                
this._sq[i].MouseMove += new MouseEventHandler (this.MouseMoveHandler);
                
this._sq[i].MouseLeave += new System.EventHandler(this.MouseLeaveHandler);
            }
        }
        
//حدث تحميل الفورم
        
private void Form1_Load(object senderEventArgs e)
        {
            
this._isGameOver false;
            
turn_num 0;
            
_sq = new Label[9] { sq1sq2sq3sq4sq5sq6sq7sq8sq9 };
        }
 
        
//هذا حدث الضغط على المربعات النصية
        
private void ClickHandler(object senderSystem.EventArgs e)
        {
            
Label tempButton = (Label)sender;
            
turn_num += 1;
            if (
tempButton.Text != "")
            {
                return;
            }
            if (
playerturn == "x")
            {
                
tempButton.ForeColor Color.Red;
                
tempButton.Text "X";
                
playerturn "o";
                
lblturn.Text "O دور اللاعب";
                
lblturn.ForeColor Color.Blue;
            }
            else
            {
                
tempButton.ForeColor Color.Blue;
                
tempButton.Text "O";
                
playerturn "x";
                
lblturn.Text "X دور اللاعب";
                
lblturn.ForeColor Color.Red;
            }
            
tempButton.BackColor Color.Transparent
            
this._isGameOver CheckWinner(_sq);
            if (
_isGameOver == true)
            {
                
startgame();
            }
            if (
turn_num == _isGameOver == false)
            {
                
MessageBox.Show("تعااااادل""انتهت اللعبة"MessageBoxButtons.OK);
                
startgame();
            }
        }

        static private 
int[,] Winners = new int[,]
{
{
0,1,2},
{
3,4,5},
{
6,7,8},
{
0,3,6},
{
1,4,7},
{
2,5,8},
{
0,4,8},
{
2,4,6}
};
        

       
//هذا حدث معرفة الفائز
        
static public bool CheckWinner(Label[] myControls)
        {
            
bool gameOver false;
            for (
int i 08i++)
            {
                
int a Winners[i0], Winners[i1], Winners[i2];
                
Label b1 myControls[a], b2 myControls[b], b3 myControls[c];
                if (
b1.Text == "" || b2.Text == "" || b3.Text == "")
                    continue;
                if (
b1.Text == b2.Text && b2.Text == b3.Text)
                {
                    
b1.BackColor b2.BackColor b3.BackColor Color.Lime;
                    
gameOver true;
                    
MessageBox.Show(b1.Text "  ... لقد فاز اللاعب ""انتهت اللعبة"MessageBoxButtons.OK);
                    break;
                }
            }
            return 
gameOver;
        }
    }

المشروع و الصورة

للتحميل اضغط هنا
متغيب قليلاً للدراسة Smile
}}}
تم الشكر بواسطة:



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم