Programming a Tic-Tac-Toe Game in C# - Full Guide

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ก.พ. 2025

ความคิดเห็น • 74

  • @OttoBotCode
    @OttoBotCode  ปีที่แล้ว +5

    *Source code*
    If you don't want to write the code yourself, it's available here for 3$ (and free for Patreon supporters):
    www.patreon.com/OttoBotCode/shop/78705
    Your support would be a massive help for me and my channel! 🤩

    • @Demiurge_Ze029
      @Demiurge_Ze029 ปีที่แล้ว

      Hey bro can you help me i having problems with the ImageControls[r, c] = imageControls; it says that [] connot apply indexing an expression of type "image" error

    • @Demiurge_Ze029
      @Demiurge_Ze029 ปีที่แล้ว

      Respond Bro Need it Pls :)

  • @RizomTV-6IX
    @RizomTV-6IX 9 หลายเดือนก่อน +2

    Iam nowhere near this progress in my course about learning c#, but still went through the full video and did not only solve my understanding problem, but also learned tons of other things. Thank you very much!!

  • @chrisburns8347
    @chrisburns8347 25 วันที่ผ่านมา

    Absolutely Amazing Video! Comprehensive Use of WPF! Awesome! Excellent programming Practices! Going to Use Your Video in My College WinForms / WPF Programming Class as a Target to Aim for! Hope You Don't mind being the "Measurement Stick" of Expectation.

    • @chrisburns8347
      @chrisburns8347 23 วันที่ผ่านมา

      I have a full list of compilation errors if that helps you.

  • @ЛазарЙорданов-й7щ
    @ЛазарЙорданов-й7щ 2 ปีที่แล้ว +5

    Great guide! I hope one day i will reach your level and beyond. :)

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว +1

      Thank you! You will for sure 😀

  • @abhinavprabhakar455
    @abhinavprabhakar455 3 ปีที่แล้ว +4

    Hey Man! It's Just amazing
    Keep it up!
    You'll get your subscribers eventually!

    • @OttoBotCode
      @OttoBotCode  3 ปีที่แล้ว +1

      Thank you 😃

    • @alexanderbozhko
      @alexanderbozhko 5 หลายเดือนก่อน

      ​@@OttoBotCode When the End Screen is gonna appear, the fade in is disabled, and when i click Play Again, Fade in also Disabled in the Game Screen.
      Can you tell me how to fix it? Thanks for advance.

  • @kayaikinci9855
    @kayaikinci9855 7 หลายเดือนก่อน

    You're life saver bro, many thanks

  • @ItsWeiWei
    @ItsWeiWei 3 ปีที่แล้ว +1

    So underrated!! You are amazing!

    • @OttoBotCode
      @OttoBotCode  3 ปีที่แล้ว

      I appreciate it! Thanks alot 😉

  • @rizveahmad5293
    @rizveahmad5293 11 หลายเดือนก่อน

    Love it bro. Thanks for the help

  • @SmallScaleFun
    @SmallScaleFun 2 ปีที่แล้ว +1

    Hey!
    Can you please help me fix this error? (after you first build the project approx. 29:00) I try to build the project
    Error CS1061 'MainWindow' does not contain a definition for 'GameGrit_MouseDown' and no accessible extension method 'GameGrit_MouseDown' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?)
    Thank you!

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว +1

      It cannot find the the MouseDown method in MainWindow.xaml.cs for some reason. You spelled "Grid" wrong ("Grit"). Maybe your method is called "GameGrid_MouseDown", but it is registered as "GameGrit_MouseDown" in the xaml.
      If this doesn't help, then send your entire project (zip or github link) to ottobotcodehelp@gmail.com, then I'll take a look 😊

    • @SmallScaleFun
      @SmallScaleFun 2 ปีที่แล้ว +1

      @@OttoBotCode LOOL, you have eagle eyes, I change the method to "Grit" and now is working. Thank you for taking the time and check my problem :)

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว +2

      @@SmallScaleFun That's what years of staring at code will do to you 😆 No problem, I'm glad it works!

    • @SmallScaleFun
      @SmallScaleFun 2 ปีที่แล้ว

      @@OttoBotCode :)) I'm just starting to learn C# OOP and I was thinking that your project will be a good exercise. Thank you again for your time.

  • @רועיאטיאס-ל2ע
    @רועיאטיאס-ל2ע ปีที่แล้ว +1

    Great tutorial!
    I need help with the Uri, 24:00, Ot doesn't locate my image. Anyone know how to find it.

  • @demillion2183
    @demillion2183 ปีที่แล้ว

    hello i get a Compiler Error CS0116 at 24:00 at the line of code of" private readonly Dictionary imageSources = new () " how do I fix this? am I missing something

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว

      You are using an old version of C#. If you write the type again after new it should work:
      private readonly Dictionary imageSources = new Dictionary()

  • @pedroleite5469
    @pedroleite5469 14 วันที่ผ่านมา

    Thank you

  • @Harrisz777
    @Harrisz777 8 หลายเดือนก่อน +1

    love the video!!
    I want to add another Option to the Game To play Against CPU.
    Can you help me with that??

  • @christianjrgensen3857
    @christianjrgensen3857 2 ปีที่แล้ว

    Takk!

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว

      Det er mig som takker! 😀

  • @anasalbadi1792
    @anasalbadi1792 ปีที่แล้ว

    man you are legend!

  • @nutualexandra8685
    @nutualexandra8685 ปีที่แล้ว

    Hi
    I bought the game from you with x and a but I can't open it
    one or more projects in the solution were not loaded correctly .
    Please see the output window for details
    I don t know know how to solve this problem
    please help me

  • @communism8037
    @communism8037 2 ปีที่แล้ว +2

    13:57 You invoke the event in the if, and in the else statements. Is it a good solution? Maybe you should write invoke-command before the if-else?

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว +2

      I can see where you are coming from but there's a good reason for doing it this way.
      When the event is invoked the event handler for it is called immediately.
      If we invoke the event before the if-else, the code in the event handler will see the wrong current player (it won't have been updated in time).
      If we invoke it after the if-else, the game ended event is invoked before the final move of the game, which is also bad.
      Hope that makes sense 😊

    • @communism8037
      @communism8037 2 ปีที่แล้ว +1

      @@OttoBotCode interesting, thank you

  • @joshfraser3991
    @joshfraser3991 2 ปีที่แล้ว +1

    Amazing, thanks!

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว

      Thank you for the nice comment 😊

  • @smarterlife7331
    @smarterlife7331 ปีที่แล้ว

    Good one. Thanks.

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว

      Glad you like it. Thanks for the comment 😄

  • @daniyaramirov7269
    @daniyaramirov7269 8 หลายเดือนก่อน

    Awesome guide!
    Could you please help me with an error I encounter🙏? In the "ShowLine()" method on the lines:
    {
    Line.BeginAnimation(Line.X2Property, x2Animation);
    Line.BeginAnimation(Line.Y2Property, y2Animation);
    }
    I have a compile error:
    "(field) static readonly DependencyProperty System.Windows.Shapes.Line.X2Property
    Identifies the System.Windows.Shapes.Line.X2 dependency property.
    CS0176: Member 'Line.X2Property' cannot be accessed with an instance reference; qualify it with a type name instead"
    In my "MainWidnow" class I have the following imports:
    {
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    }

    • @daniyaramirov7269
      @daniyaramirov7269 8 หลายเดือนก่อน

      Already fixed it by adding:
      {
      using System.Windows.Shapes;
      ...
      Line.BeginAnimation(Line.X2Property, x2Animation);
      Line.BeginAnimation(Line.Y2Property, y2Animation);
      }
      Or alternatively:
      {
      using static System.Windows.Shapes.Line;
      ...
      Line.BeginAnimation(X2Property, x2Animation);
      Line.BeginAnimation(Y2Property, y2Animation);
      }

  • @PhysiquePablo
    @PhysiquePablo ปีที่แล้ว

    am i able to email you about troubleshooting a roadblock a came across creating this?
    It would be awesome thanks

  • @lyliangouesdeux835
    @lyliangouesdeux835 11 หลายเดือนก่อน

    How did you make your assets please ? They are clean.

    • @OttoBotCode
      @OttoBotCode  11 หลายเดือนก่อน

      They are made in Adobe Illustrator!

  • @frejandsubtolclc7935
    @frejandsubtolclc7935 ปีที่แล้ว

    umm hi Otto what do i need use instead for private readonly Dictionary imageSources = (new)() the new doesnt work it says that i need hihger version 9.0 or greater

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว +1

      Hello! For older versions of C# you must write:
      private readonly Dictionary imageSources = new Dictionary() {
      // Key value pairs here
      };

    • @frejandsubtolclc7935
      @frejandsubtolclc7935 ปีที่แล้ว

      ty@@OttoBotCode

    • @frejandsubtolclc7935
      @frejandsubtolclc7935 11 หลายเดือนก่อน

      @@OttoBotCode is says this when i put the thing u sent me Severity Code Description Project File Line Suppression State
      Error CS1922 Cannot initialize type 'BitmapImage' with a collection initializer because it does not implement 'System.Collections.IEnumerable' C:\Users\Frej\source
      epos\Tic-Tac-Toe\Tic-Tac-Toe\MainWindow.xaml.cs 24
      Error CS7036 There is no argument given that corresponds to the required formal parameter 'value' of 'Dictionary.Add(Player, ImageSource)' Tic-Tac-Toe C:\Users\Frej\source
      epos\Tic-Tac-Toe\Tic-Tac-Toe\MainWindow.xaml.cs 24 Active
      Error CS1922 Cannot initialize type 'BitmapImage' with a collection initializer because it does not implement 'System.Collections.IEnumerable' Tic-Tac-Toe C:\Users\Frej\source
      epos\Tic-Tac-Toe\Tic-Tac-Toe\MainWindow.xaml.cs 24 Active
      Error CS7036 There is no argument given that corresponds to the required formal parameter 'value' of 'Dictionary.Add(Player, ImageSource)' C:\Users\Frej\source
      epos\Tic-Tac-Toe\Tic-Tac-Toe\MainWindow.xaml.cs 23
      Error CS7036 There is no argument given that corresponds to the required formal parameter 'value' of 'Dictionary.Add(Player, ImageSource)' C:\Users\Frej\source
      epos\Tic-Tac-Toe\Tic-Tac-Toe\MainWindow.xaml.cs 24
      Error CS7036 There is no argument given that corresponds to the required formal parameter 'value' of 'Dictionary.Add(Player, ImageSource)' Tic-Tac-Toe C:\Users\Frej\source
      epos\Tic-Tac-Toe\Tic-Tac-Toe\MainWindow.xaml.cs 23 Active

    • @Viktoria-ud1rk
      @Viktoria-ud1rk 11 หลายเดือนก่อน

      Hi we had the same error and tried to fix it with your answer but there is an error code : CS1001
      We don’t know what we did wrong, the rest is exactly like in the video

    • @Viktoria-ud1rk
      @Viktoria-ud1rk 11 หลายเดือนก่อน

      The coding is as follows:
      private readonly Dictionary imageSources = new Dictionary() { // Key value pairs here};
      { ❌
      { Player.X, new BitmapImage(new Uri("pack://application:,,,/Assets/X15.png" )) },
      { Player.O, new BitmapImage(new Uri("pack://application:,,, /Assets/015.png"))}
      };
      Where the emoji (❌) is set, there is the error with the code CS1001 (Identifier expected)
      Where is the mistake?

  • @SarcasticCupcake223
    @SarcasticCupcake223 2 ปีที่แล้ว +1

    Hey Again! I liked the Snake game one so much that I decided to try another one! but I have run into another issue. it wont let me start the game like you did at 29:08 because its having trouble finding the files from the Uri thing you write at 24:05. I made sure I've typed everything right as far as I can tell.

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว

      Can you post the error you get when you start the program? Maybe your assets are not marked as resources (check 15:35). You can also copy-paste the URIs from the projects Github repository (link in description) to make sure they are right 😊

    • @SarcasticCupcake223
      @SarcasticCupcake223 2 ปีที่แล้ว

      @@OttoBotCode The error only pops up when I try to run the game, and it highlights the URIs and says "Exception User-Unhandled
      System.IO.IOExeption: 'cannot locate recource 'assets/015.png'.'

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว

      @@SarcasticCupcake223 I think I need your project again. You know the drill 😆 I have a few minutes now so I can check it out!

    • @SarcasticCupcake223
      @SarcasticCupcake223 2 ปีที่แล้ว

      @@OttoBotCode Oh I just figured out what went wrong, I seemed to have typed "015" instead of "O15" lol it works now, sorry for taking up your time lmao but thank you for being so helpful!

  • @Page1992.
    @Page1992. 11 หลายเดือนก่อน

    I don't have the app.xami folder, what do I do?

    • @OttoBotCode
      @OttoBotCode  11 หลายเดือนก่อน

      Maybe you accidentally created a "Class Library" or a "Console Application". Create a new project using the "WPF Application" template, that will add the App and MainWindow classes automatically. You can copy paste the code you've already written into this new project.

  • @Boy-fv6rm
    @Boy-fv6rm 2 ปีที่แล้ว

    I have a bug the players are not switching so im only playing as X
    May i know what to do to fix it

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว

      I should be able to find the problem if I could see your code. You can send your entire project (GitHub link is fine) to ottobotcodehelp@gmail.com 😀

    • @Boy-fv6rm
      @Boy-fv6rm 2 ปีที่แล้ว +1

      @@OttoBotCode ok I sent it

  • @notohkae
    @notohkae ปีที่แล้ว

    30:54

  • @douwe3448
    @douwe3448 ปีที่แล้ว

    Doesn't work, it asks if i want to install .NET but i already have .NET

  • @jiraffe8950
    @jiraffe8950 2 ปีที่แล้ว

    Hello, dude. Help me please:
    private void onMoveMade(int _row, int _column)
    {
    Player player = gameState.gameGrid[_row, _column];
    imageControls[_row, _column].Source = imageSources[player];
    PlayerImage.Source = imageSources[gameState.currentPlayer];
    }
    it shows me: CS0103 The name "PlayerImage" does not exist in the current context.
    idk what i should to do. Check own and your code - everything is cool, not including my codestyle :(
    P.S. : Thank you for a video :3

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว

      You are very welcome 😀
      Sounds like it cannot find anything with the name "PlayerImage". Perhaps you have called it something else in MainWindow.xaml?
      In my code I have this property on the Image control inside the TurnPanel:
      x:Name="PlayerImage"
      You should also have this. If you use a different name then you must use that name in the code-behind as well.

    • @jiraffe8950
      @jiraffe8950 2 ปีที่แล้ว

      ​@@OttoBotCode OMG, dude... Thank you very much, you're the best!!! I just wrote CurrentPlayerImage and didn't notice it 😅.
      Also I want to say, that you have a the best tutorials on TH-cam!!! You explain very well. Dont't stop and keep it up! Good luck. You get a new subscriver, which will be wait your new videos)))
      P.S. : sorry for my, maybe, bad/uncorrect English, I just from Ukraine, so don't know English pretty well🙃

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว +1

      @@jiraffe8950 Thank you so much ☺ I really appreciate it 😀 And don't worry about me stopping, there is actually another C# project coming soon!

    • @jiraffe8950
      @jiraffe8950 2 ปีที่แล้ว

      @@OttoBotCode Hurrah😂😂!!!

    • @jiraffe8950
      @jiraffe8950 2 ปีที่แล้ว

      @@OttoBotCode Dude, I respect you for your code (async, await it's pretty good choice), but you violate SOLID rules, which I noticed. I'ma going to fix it and, maybe post on GitHub profile this project (I'll stay a link to this video). Don't you mind😬?

  • @davidemitra3126
    @davidemitra3126 2 ปีที่แล้ว

    awesome video

    • @OttoBotCode
      @OttoBotCode  2 ปีที่แล้ว

      I'm glad you like it 😀

  • @extrasmartextrasmart514
    @extrasmartextrasmart514 3 ปีที่แล้ว

    I typed this correctly:
    private readonly Dictionary imageSources = new()
    {
    { Player.X, new BitmapImage(new Uri("pack://application:,,,/Assets/X15.png")) },
    { Player.O, new BitmapImage(new Uri("pack://application:,,,/Assets/O15.png")) },
    };
    but it gives me an error can you help?

    • @OttoBotCode
      @OttoBotCode  3 ปีที่แล้ว

      I would love to try, can you tell me what kind of error you are getting?

    • @OttoBotCode
      @OttoBotCode  3 ปีที่แล้ว +2

      Let me try to guess the problem.
      This line:
      private readonly Dictionary imageSources = new()...
      Would usually be written like this:
      private readonly Dictionary imageSources = new Dictionary()...
      I have omitted the type after the "new" keyword (I found it a little too verbose). This feature was introduced in C# 9, so if you are using an older version of C#, it won't work. Either use the "old" way where you explicitly write the type again after "new", or use C# version 9 or greater.
      I hope that solves the problem, otherwise just let me know.

    • @aydnyusifov1704
      @aydnyusifov1704 2 ปีที่แล้ว +1

      @@OttoBotCode Thank you, i had same problem that fixed with this code