1 Kasım 2018 Perşembe

C Sharp Form Sos Oyunu Programı



KONU : C Sharp Uygulamalar - C Sharp ( C# ) formda sos oyunu programı yapımı. c sharp iç içe for döngüsü kullanarak 3x3 matriks alanı oluşturma. C sharp da dinamik olarak textbox oluşturup form üzerine ekleme. C sharp forma eklenen textbox nesnelerini iki boyutlu bir diziye atama. Foreach kullanarak form kontrolleri üzerinde gezinerek textbox kutucuklarının içeriğini temizleme. c sharp try catch kullanılarak hata ile karşılaşılan durumları engelleme. Exception durumunu engelleme örneği. C sharp 3x3 matrikli sos oyunu programı oluşturma.
ETİKETLER: c sharp form - c sharp sos - c sharp sos oyunu - c sharp game - c sharp try catch - c sharp for - csharp foreach



UYGULAMAYI İNDİR

 


 

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 sos
{
    public partial class Sos_Oyunu : Form
    {
      
        public Sos_Oyunu()
        {
            InitializeComponent();
        }

        TextBox[,] alanlar;
        int birinci_oyuncu = 0, ikinci_oyuncu = 0;
        int satır_sayısı = 0, sütun_sayısı = 0;
                  
        private void Form_oyun_Load(object sender, EventArgs e)
        {
            /// Sos oyun alanı 3 x 3 matriksi çeklinde olsun
            /// 
            satır_sayısı = 3;
            sütun_sayısı = 3;

            alanlar = new TextBox[satır_sayısı, sütun_sayısı];

            /// iç içe for döngüsü kullanarak sos oyun alanını textbox lar kullanarak oluşturalım

            for (int i = 0; i < satır_sayısı; i++)
            {
                for (int j = 0; j < sütun_sayısı; j++)
                {

                    TextBox yenitextBox = new TextBox();
                    yenitextBox.Location = new System.Drawing.Point(50 + j * 25, i * 25 + 50);
                    yenitextBox.Name = i + " * " + j;
                    yenitextBox.Size = new System.Drawing.Size(20, 20);
                    yenitextBox.TabIndex = 1;
                    yenitextBox.BorderStyle = BorderStyle.FixedSingle;
 
                    yenitextBox.KeyUp += new KeyEventHandler(yenitextBox_KeyUp);
                    this.Controls.Add(yenitextBox);
                    alanlar[i, j] = yenitextBox;


                }
            }
        }

        private void yenitextBox_KeyUp(object sender, KeyEventArgs e)
        {
 
            ///Hangi textbox kutucuğuna karakter girilmiş ise o kutuyu referans alalım
            
            TextBox text = sender as TextBox;
            label1.Focus();

            ///Eğer textbox kutucuguna birden fazla karakter girilmiş ise sadece ilk karakteri
            ///dikkate alalım

            if (text.TextLength > 1)
            {
                text.Text = text.Text.Substring(0, 1);

            }

            text.Text = text.Text.ToUpper();

            ///Eğergirilen karakter S veya o değilse textbox kutucuğunu temizleyelim
            
            if (text.Text != "S" && text.Text != "O")
            {
                text.Text = "";
            }

            ///Oyun da SOS oluştu mu kontrol edelim
           
            for (int i = 0; i < satır_sayısı; i++)
            {
                for (int j = 0; j < sütun_sayısı; j++)
                {
                    bool sosOlduMu = false;

                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i, j + 1].Text == "O" && alanlar[i, j + 2].Text == "S")
                            sosOlduMu = true;
                    }
                    catch (Exception) { }
                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i, j - 1].Text == "O" && alanlar[i, j - 2].Text == "S")
                            sosOlduMu = true;
                    }
                    catch (Exception) { }
                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i + 1, j].Text == "O" && alanlar[i + 2, j].Text == "S")
                            sosOlduMu = true;
                    }
                    catch (Exception) { }
                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i - 1, j].Text == "O" && alanlar[i - 2, j].Text == "S")
                            sosOlduMu = true;
                    }
                    catch (Exception) { }
                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i + 1, j + 1].Text == "O" && alanlar[i + 2, j + 2].Text == "S")
                            sosOlduMu = true;
                    }
                    catch (Exception) { }
                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i + 1, j - 1].Text == "O" && alanlar[i + 2, j - 2].Text == "S")
                            sosOlduMu = true;
                    }
                    catch (Exception) { }
                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i - 1, j - 1].Text == "O" && alanlar[i - 2, j - 2].Text == "S")
                            sosOlduMu = true;
                    }
                    catch (Exception) { }
                    try
                    {
                        if (alanlar[i, j].Text == "S" && alanlar[i - 1, j + 1].Text == "O" && alanlar[i - 2, j + 2].Text == "S")


                            sosOlduMu = true;
                    }
                    catch (Exception) { }


                    if (sosOlduMu)
                    {
                        MessageBox.Show("SOS :" + text.Text + " OYUNCUSU OYUNU KAZANDI.", "TEBRİKLER", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    }
                }
            }
        }

        private void buttonYeniOyun_Click(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                if (item is TextBox)
                {
                    (item as TextBox).Text = "";
                }
            }
        }
    }

}

 

 

UYGULAMAYI İNDİR

C Sharp Form Sos Oyunu Programı


C Sharp Uygulamalar Konsolda Sayı Tahmin Etme Oyunu Oyna


C Sharp Form Uygulamalar Süreli Sayı Tahmin Oyunu


C Sharp Uygulamalar Haritadan Şehir İsmi Bulma Oyunu Oyna


C Sharp Form Uygulamalar Puzzle Oyunu Oyna


C Sharp Form İstenilen Alan Kullanılarak Sos Oyunu Programıa


C Sharp Form Sos Oyunu 5x5