📜 ⬆️ ⬇️

Autonumber in four languages. Part 1


Small introduction


I got into the IT world relatively recently: I have been developing iOS applications for only two years. But besides Objective-C and Swift, the world of Java and C # has always attracted me. Periodically, I allocated time to watch some videos that teach the basics of these languages, but it didn’t go further than simply viewing and rewriting the code from the screen. And then I remembered a mathematical game that my friend once suggested to me.


The essence of the game is as follows: you walk down the street and look at car numbers. And in each number you count the sum of all numbers (for example, in number 8037 you need to count 8 + 0 + 3 + 7). This is the easiest level of the game. The second most difficult level is to calculate the sum of the first half of the number and the second (80 + 37). There is a third level - multiply all the digits (with zero zeros: 8 x 3 x 7) and the fourth - multiply the first half of the number by the second (80 x 37).


In general: I decided to write this game (in the console version) in four languages: Swift, Objective-C, Java and C #. What came out of it? Let's get a look.


Where do we start?


We will start by creating empty projects for console applications: for Swift and Objective-C we will use Xcode, for Java - naturally IntelliJ IDEA, and for C # - Xamarin Studio.


The first thing to do is write a GameStart helper class. He will deal with the request from the user until he enters a keyword to exit the application.


Swift


Create the class itself:


 class GameStart { } 

In it, we will have the exitWord property and initializer:


 private var exitWord: String init(with exitWord: String) { self.exitWord = exitWord } 

This will be our keyword.


It will also have a startGame method that will constantly ask the user for an answer until he enters a word to exit:


 func startGame() { print(GreetingMessage.replacingOccurrences(of: ExitWordPlaceholder, with: self.exitWord)) guard let inputWord = readLine() else { print(ErrorMessage) return } self.check(inputWord: inputWord) } private func check(inputWord: String) { if inputWord == self.exitWord { print(GoodByeMessage) } else { print(InputAcceptMessage.replacingOccurrences(of: InputWordPlaceholder, with: inputWord)) startGame() } } 

As you can see, the startGame method welcomes the user, then reads from the command line what the user has entered, and passes the resulting string to the check(inputWord:) method.


String constants that were used:


 private let ExitWordPlaceholder = "{exitWord}" private let InputWordPlaceholder = "{inputWord}" private let GreetingMessage = "Please enter your answer (enter \"\(ExitWordPlaceholder)\" for exit):" private let InputAcceptMessage = "You entered \"\(InputWordPlaceholder)\".\n" private let GoodByeMessage = "Good bye.\n" private let ErrorMessage = "There is unknown error, sorry. Good bye.\n" 

Our class is ready, now we need to create an object and call the startGame() method:


 let gameStart = GameStart(with: "quit") gameStart.startGame() 

In the console, it looks like this:



Now we will write the same class in Objective-C:


 //   GameStart.h @interface GameStart : NSObject - (instancetype)initWithExitWord:(NSString *)exitWord; - (void)startGame; @end //   GameStart.m const NSString *GreetingMessage = @"Please enter your answer (enter \"%@\" for exit):"; const NSString *InputAcceptMessage = @"You entered \"%@\".\n"; const NSString *GoodByeMessage = @"Good bye.\n"; const NSString *ErrorMessage = @"There is unknown error, sorry. Good bye.\n"; @interface GameStart() @property (strong, nonatomic) NSString *exitWord; @end @implementation GameStart - (instancetype)initWithExitWord:(NSString *)exitWord { self = [super init]; if (self) { self.exitWord = exitWord; } return self; } - (void)startGame { NSLog(GreetingMessage, self.exitWord); NSString *inputWord = [self readLine]; if (inputWord) { [self checkInputWord:inputWord]; } else { NSLog(@"%@", ErrorMessage); } } - (void)checkInputWord:(NSString *)inputWord { if ([inputWord isEqualToString:self.exitWord]) { NSLog(@"%@", GoodByeMessage); } else { NSLog(InputAcceptMessage, inputWord); [self startGame]; } } - (NSString *)readLine { char inputValue; scanf("%s", &inputValue); return [NSString stringWithUTF8String:&inputValue]; } @end 

Well, creating an object with a method call:


 GameStart *gameStart = [[GameStart alloc] initWithExitWord:@"quit"]; [gameStart startGame]; 

Next up is Java.


GameStart class:


 public class GameStart { private static final String GreetingMessage = "Please enter your answer (enter \"%s\" for exit):"; private static final String InputAcceptMessage = "You entered \"%s\".\n"; private static final String GoodByeMessage = "Good bye.\n"; private String exitWord; public GameStart(String exitWord) { this.exitWord = exitWord; } void startGame() { System.out.println(String.format(GreetingMessage, exitWord)); String inputWord = readLine(); checkInputWord(inputWord); } private void checkInputWord(String inputWord) { if (inputWord.equals(exitWord)) { System.out.println(GoodByeMessage); } else { System.out.println(String.format(InputAcceptMessage, inputWord)); startGame(); } } private String readLine() { java.util.Scanner scanner = new java.util.Scanner(System.in); return scanner.next(); } } 

And the challenge:


 GameStart gameStart = new GameStart("quit"); gameStart.startGame(); 

And completes the top four C #


Class:


 public class GameStart { const string GreetingMessage = "Please enter your answer (enter \"{0}\" for exit):"; const string InputAcceptMessage = "You entered \"{0}\".\n"; const string GoodByeMessage = "Good bye.\n"; readonly string exitWord; public GameStart(string exitWord) { this.exitWord = exitWord; } public void startGame() { Console.WriteLine(string.Format(GreetingMessage, exitWord)); string inputWord = Console.ReadLine(); checkInputWord(inputWord); } void checkInputWord(string inputWord) { if (inputWord.Equals(exitWord)) { Console.WriteLine(GoodByeMessage); } else { Console.WriteLine(string.Format(InputAcceptMessage, inputWord)); startGame(); } } } 

Call:


 GameStart gameStart = new GameStart("quit"); gameStart.startGame(); 

A little random doesn’t hurt


Also add to the project auxiliary class, which will form our car number (the number should consist of four random numbers). The class itself is simply called the Randomizer .


Swift:


 class Randomizer { var firstNumber: UInt32 var secondNumber: UInt32 var thirdNumber: UInt32 var fourthNumber: UInt32 init() { self.firstNumber = arc4random() % 10 self.secondNumber = arc4random() % 10 self.thirdNumber = arc4random() % 10 self.fourthNumber = arc4random() % 10 } } 

Objective-C:


 //   Randomizer.h @interface Randomizer : NSObject @property (assign, nonatomic) NSInteger firstNumber; @property (assign, nonatomic) NSInteger secondNumber; @property (assign, nonatomic) NSInteger thirdNumber; @property (assign, nonatomic) NSInteger fourthNumber; @end //   Randomizer.m @implementation Randomizer - (instancetype)init { self = [super init]; if (self) { self.firstNumber = arc4random() % 10; self.secondNumber = arc4random() % 10; self.thirdNumber = arc4random() % 10; self.fourthNumber = arc4random() % 10; } return self; } @end 

Java:


 public class Randomizer { private static Random random = new Random(); int firstNumber; int secondNumber; int thirdNumber; int fourthNumber; public Randomizer() { firstNumber = random.nextInt(10); secondNumber = random.nextInt(10); thirdNumber = random.nextInt(10); fourthNumber = random.nextInt(10); } } 

C #:


 public class Randomizer { static readonly Random random = new Random(); public int firstNumber; public int secondNumber; public int thirdNumber; public int fourthNumber; public Randomizer() { firstNumber = random.Next(10); secondNumber = random.Next(10); thirdNumber = random.Next(10); fourthNumber = random.Next(10); } } 

As you can see, during initialization, we simply fill four fields in the class with random integers from 0 to 9.


Universality is our all


Despite the fact that there will be only one game in our application, we will pretend that we are providing for extensibility of the application in the future. Therefore, we add an interface (for Swift and Objective-C - protocol) Game with the methods greet(with exitWord: String) and check(userAnswer: String) .


Swift:


 protocol Game { func greet(with exitWord: String) func check(userAnswer: String) } 

Objective-C:


 @protocol Game <NSObject> - (void)greetWithExitWord:(NSString *)exitWord; - (void)checkUserAnswer:(NSString *)userAnswer; @end 

Java:


 public interface Game { void greet(String exitWord); void checkUserAnswer(String userAnswer); } 

C #:


 public interface IGame { void greet(string exitWord); void checkUserAnswer(string userAnswer); } 

On it I will finish the first part. Realization of the game itself with the choice of the level of difficulty, checking the player’s response to correctness, with blackjack and ... we will do in the second part . All good. :)


')

Source: https://habr.com/ru/post/326568/


All Articles