OO for computer in C #

Source: Internet
Author: User

Some time ago to see someone "about a C # on the computer Problem oo", and then someone used the decorative mode to do this problem, I here to a strategy model, not accustomed to nonsense directly on the code, do not know how to calculate the strategy model, please expert advice.

1 using System;


2 using System.Collections.Generic;


3 using System.Linq;


4 namespace ConsoleApp


5 {


6 public class program


7 {


8 static void Main (string[] args)


9 {


Game Game = new Game (17);


11//Set Strategy


game. Strategy = new Strategya ();


game. Gameover + = new EventHandler (game_gameover);


game. Start ();


Console.read ();


16}


static void Game_gameover (object sender, EventArgs e)


18 {


Console.WriteLine ("Last Person:" + sender);


20}


21}


///<summary>


23///Policy Interface


///</summary>


public Interface IStrategy


26 {


///<summary>


28///running Strategy


///</summary>


///<param name= "G" ></param>


to void Work (Game g);


32}


///<summary>


34///Strategy a


35///from the first person start off, report a multiple exit of 3, until the last one left, with object-oriented thinking to do this problem.


///</summary>


Notoginseng public class Strategya:istrategy


38 {


private list<person> over = new list<person> ();


private int k = 0;


public void Work (Game Game)


42 {


Game (Person p in). Players)


44 {


P.say (++k);


if (k% 3 = 0)


over. ADD (P);


48}


game. Players.removeall (o => over. Contains (o));


50}


51}


delegate void EventHandler (object sender, EventArgs e);


///<summary>


54///Game


///</summary>


public class Game


57 {


public Game (int num)


59 {


players = new list<person> ();


for (int i = 0; i < num; i++) {


Players.add (New person (i + 1));


63}


64}


///<summary>


66///Game Strategy


///</summary>


istrategy Strategy {get; set;}


///<summary>


70///Game player


///</summary>


list<person> Players {get; set;}


///<summary>


74///Game End Event


///</summary>


the public event EventHandler Gameover;


///<summary>


78///Start the game


///</summary>


public void Start ()


81 {


if (strategy!= null)


83 {


(Players.count > 1)


85 {


Strategy.work (this);


87}


Gameover (this. Players.first (). Id, New EventArgs ());


89}


90}


91}


///<summary>


93///Player


///</summary>


public class Person


96 {


(int id)


98 {


this. id = ID;


100}


///<summary>


102///Player ID


///</summary>


the public int Id {get; set;}


///<summary>


106///Players off


///</summary>


108///<param name= "num" ></param>


109 public void Say (int num)


110 {


Console.WriteLine (String. Format ("{0}:{1}", Id, num));


112}


113}


114}


115

Strategya Implement Interface IStrategy follow the open and closed principle, if we want to change a rule just add a class implementation istrategy can.

Before the use of circular linked list to complete the problem, the evening again sent up.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.