问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacetest3{classChess{privateint[,]a=newint[10,10];publicChess(){for(inti=0;i<10;i++){for(intj=0;j<10;j++)a[i,j]=0;}}publicvoidsetChess(intx,inty){a[x-1,y-1]=1;}publicvoidShow(){for(inti=0;i<10;i++){for(intj=0;j<10;j++){if(a[i,j]==1)Console.Write("X");elseConsole.Write("O");}Console.WriteLine();}}}classOriginator{privateintx,y;privateChesschess1=newChess();publicvoidOsetChess(){chess1.setChess(x,y);}publicintX{get{returnx;}set{x=value;}}publicintY{get{returny;}set{y=value;}}publicMementoCreateMemento(){return(newMemento(x,y,chess1));}publicvoidSetMemento(Mementomemento){x=memento.X;y=memento.Y;chess1=memento.Chess;}publicvoidShow(){Console.WriteLine("X="+x);Console.WriteLine("Y="+y);chess1.Show();}}classMemento{privateintx,y;privateChesschess2=newChess();publicMemento(intx,inty,Chesschess){this.x=x;this.y=y;this.chess2=chess;}publicChessChess{get{returnchess2;}set{chess2=value;}}publicintX{get{returnx;}set{x=value;}}publicintY{get{returny;}set{y=value;}}}classCaretaker{privateMementomemento;publicMementoMemento{get{returnmemento;}set{memento=value;}}}classProgram{staticvoidMain(string[]args){Originatora=newOriginator();a.X=2;a.Y=3;a.OsetChess();Console.WriteLine("a选手下棋前");a.Show();Caretakerc=newCaretaker();c.Memento=a.CreateMemento();a.X=3;a.Y=4;c.Memento.Chess.Show();a.OsetChess();c.Memento.Chess.Show();Console.WriteLine("a选手下棋后");a.Show();a.SetMemento(c.Memento);Console.WriteLine("a选手选择悔棋");Console.WriteLine("a选手悔棋成功");a.Show();Console.ReadKey();}}}